DEV Community

Jack Lin
Jack Lin

Posted on • Updated on

Develop .NET Core 3.1 Inside a Container

Distros like Ubuntu 22.04 only supports OpenSSL 3, so users can only install .NET 6 SDK, it does not support .NET Core 3.1 or 2. Fortunately, Microsoft officially provides image files for development, and we don't even need to write Dockerfile to set up the environment.

Setup Environment

Here are the instructions to start a container named dotnet31 locally using devcontainers/dotnet:dev-3.1:

$ mkdir -p dev-container/dotnet31
$ cd dev-container
$ docker pull mcr.microsoft.com/devcontainers/dotnet:dev-3.1
$ docker run -it --name dotnet31 --volume ./dotnet31:/workspace:cached --network host mcr.microsoft.com/devcontainers/dotnet:dev-3.1 sleep infinity
Enter fullscreen mode Exit fullscreen mode

Notice that we use --network host, that makes the container share the host’s networking namespace. So be careful to do any network change inside the container. You can omit the option if you don't need to access localhost from the container.

Next, enter the container to test whether the program can be compiled and executed:

$ docker exec -it dotnet31 bash
Enter fullscreen mode Exit fullscreen mode

Inside the container:

# dotnet --list-sdks
3.1.425 [/usr/share/dotnet/sdk]
# mkdir /workspace/test && cd /workspace/test
# dotnet new console
# dotnet run
Hello World!
Enter fullscreen mode Exit fullscreen mode

Then use your favorite IDE or editor to enter the container development, I personally use VS Code's Dev Containers.

Other settings

Notice that you may need to manual use --environment=Development to tell the compiler using development settings.

# dotnet run --environment=Development
Enter fullscreen mode Exit fullscreen mode

You may also need to set up Entity Framework.

# dotnet tool install --global dotnet-ef --version 3.1.3
Enter fullscreen mode Exit fullscreen mode

Finally, you can also install other packages.

# sudo passwd root
# sudo apt install neofetch
Enter fullscreen mode Exit fullscreen mode

Hope this to be helpful for who annoyed at developing old .NET projects in new Ubuntu host systems.

Top comments (3)

Collapse
 
alexsiilvaa profile image
Alex Silva

I love docker and made it using docker-compose.

version: '3.4'

services:
  dotnet31:
    image: mcr.microsoft.com/devcontainers/dotnet:dev-3.1
    container_name: dotnet31
    volumes:
      - /mnt/c/repos/dev-container/dotnet31:/workspace:cached
    networks:
      - host
    command: bash -c "sleep infinity"

networks:
  host:
Enter fullscreen mode Exit fullscreen mode
Collapse
 
blueskyson profile image
Jack Lin • Edited

That's greate, thanks! I prefer to make it as simple as possible, so I didn't make a docker-compose.

Collapse
 
epradosvaliente profile image
E.Prados Valiente

@blueskyson any sample ?