Quick Linux setup for .NET development using Docker and Visual Studio Code

Small quick setup to develop applications with the .NET SDK and MSSQL on Linux. We will use Docker containers and the Visual Studio Code Dev Containers extension to create a super portable development environment!

First of all, we need to install these 3 extensions on our Visual Studio Code editor.

extensions

Before continuing, make sure you have Docker installed and working correctly on your operating system, in my case and for this post it is Fedora Linux 37. Install Docker.

Now, let's create the following project structure.

tree

Our Dockerfile should look like this.


        ARG VARIANT="7.0"
        FROM mcr.microsoft.com/vscode/devcontainers/dotnet:0-${VARIANT}

        RUN apt-get update && \
            export DEBIAN_FRONTEND=noninteractive && \
            curl -sSL https://get.docker.com/ | sh

        RUN dotnet tool install -g dotnet-ef
        ENV PATH $PATH:/root/.dotnet/tools
        # configure for https
        RUN dotnet dev-certs https
        

We are using a fully equipped container as a .NET development environment including Entity Framework. We also need to add the following variables to our sqlserver.env file.


            ACCEPT_EULA="Y"
            SA_PASSWORD="Password1"
        

Let's configure now our docker-compose.yaml file as the following


        version: '3.4'
        services:
            sql_server:
                container_name: sqlserver
                image: "mcr.microsoft.com/mssql/server:2022-latest"
                ports:
                    - "9876:1433"
                volumes:
                    -habit-db-volume:/var/lib/mssqlql/data/
            env_file:
                - sqlserver/sqlserver.env
        dev-env:
            container_name: dev-env
            build:
            context: ./dev-env
            volumes:
                - "..:/workspace"
            stdin_open: true # docker run -i
            tty: true # docker run -t
        volumes:
            habit-db-volume: null
        

Open a terminal session and from the docker directory execute the "docker compose up".


      $  cd docker
      $  docker compose up
        

Wait until the end of the process. It may take some time the first time.

tree

Great. Now that we have two containers setup, one with .NET SDK and Entity Framework and the other one with an SQL Server instance, let's intereact with them to make sure that everything works us expected.

Open another terminal session and type the next.


        $ docker exec -it dev-env /bin/bash
        $ dotnet --version
        $ dotnet-ef --version
        
term pic

And same with the MSSQL container.


        $ docker exec -it sqlserver /bin/bash
        $ /opt/mssql-tools/bin/sqlcmd -S localhost -U SA
        

Where -S means the server name and -U the login_id defined inside sqlserver.env.


        > SELECT @@VERSION
        > GO
        

You should expect and output like this:

tree

Now it's time to configure our Visual Studio environment using Dev Containers extension. This extension will take care of saving our visual studio development configuration, with all the extensions and tools (like Docker) used to develop the project and allow us to use it anyware without worrying about configurations.

Let's create a new directory inside TEST_APP folder called .devcontainer with a file called devcontainer.json (our dev containers configuration). As it follows:


    {
        "name": "TEST APP",
        "dockerComposeFile": ["../docker/docker-compose.yaml"],
        "service": "dev-env",
        "workspaceFolder": "/workspace",
        "customizations": {
            "vscode": {
                "extensions": [
                    "ms-dotnettools.csharp",
                    "shardulm94.trailing-spaces",
                    "mikestead.dotenv",
                    "fernandoescolar.vscode-solution-explorer",
                    "jmrog.vscode-nuget-package-manager",
                    "patcx.vscode-nuget-gallery",
                    "pkief.material-icon-theme",
                    "ms-mssql.mssql",
                    "humao.rest-client",
                    "rangav.vscode-thunder-client",
                    "formulahendry.dotnet-test-explorer",
                    "kevin-chatham.aspnetcorerazor-html-css-class-completion",
                    "syncfusioninc.blazor-vscode-extensions",
                    "ms-dotnettools.vscode-dotnet-runtime",
                    "ms-dotnettools.blazorwasm-companion"
                    ]
              }
        },
        "remoteUser": "root"
    }
        

These are some of the extension that we can use for .NET development with Visual Studio Code. You can remove or add more as you want.

In order to put this into work we have to enable our new Dev Container. To do that, we press F-1 key and select the following option (Rebuild and Reopen in Container).

tree

Now if you look to the bottom-left of your VSC window you will see a blue badge telling that the Dev Container TEST_APP is active. You can make sure opening a new terminal inside VSC and typing "dotnet --version". You should get the version of .NET as we just created a Dev Container of our dev-env docker container.

tree

As you can see, the terminal we just opened runs as "root". That's how we defined in our devcontainer.json with the "RemoteUser":"root" parameter.

tree

In summary, we have created a container with the .NET SDK and entity framework and on the other hand a container with the SQL Server database engine. All of this is configured using the VSC Dev Containers extension so that anyone can clone our project in detail, including the extensions that we have used in our development environment. A very effective solution that should work on any machine that supports Docker and Visual Studio Code!



Back