DevContainers .Net Setup
27 Aug 20251. Introduction
How to setup a c# solution with
- wsl2
- docker desktop
- vs code
- dev container extension
- c# dev kit
- test explorer ui
2. Ensure the following is installed
- Install WSL2 ` wsl –install` via powershell
- Install Docker Desktop
- Enable WSL2 integration in Docker Desktop
- Ensure a distro is installed and enabled. eg. Ubuntu
- Install VS Code + Extensions
3. Setup
3.1 In VSCode
- Open new VS Code window
- Connect to WSL
Press F1 and search for: "Connect to WSL"
- In the terminal, Ctrl + `, navigate to where you want to setup the folders
- Create a new folder for the solution
- Open the folder in the dev container
Press F1 and search for: "Dev Containers: Open Folder in Container"
- Navigate to the folder and click
Open
- Select appropriate C# container configuration template. Eg. C# (dev container)
- Select appropriate .net version
- Wait for the dev container to build and start up.
- Edit
.devcontainer/devcontainer.json
to be:{ "name": "Dotnet Dev Container", "build": { "dockerfile": "Dockerfile", "context": ".." }, "postCreateCommand": "dotnet restore", "customizations": { "vscode": { "extensions": [ "ms-dotnettools.csdevkit", "ms-dotnettools.vscode-dotnet-runtime", "ms-dotnettools.csharp", "formulahendry.dotnet-test-explorer" ], "settings": { "terminal.integrated.defaultProfile.linux": "bash" } } }, "remoteUser": "vscode" }
- Add
DockerFile
to ./devcontainer folderFROM mcr.microsoft.com/dotnet/sdk:9.0 # Optional: install tools you want inside devcontainer RUN apt-get update && apt-get install -y \ git \ curl \ bash \ && rm -rf /var/lib/apt/lists/* RUN useradd -m vscode && \ apt-get update && apt-get install -y sudo && \ echo "vscode ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers USER vscode
- Rebuild the container.
Press F1. Search for: "Dev Container: Rebuild Container"
3.2 Creating a new .net solution
- Open terminal. Press
Ctrl + `
- In VS Code run the following to create a new solution and new projects and tests.
dotnet new sln -n MySolution
dotnet new classlib -n Api
dotnet new xunit -n Api.Tests
dotnet sln add Api
dotnet sln add Api.Tests
dotnet add Api.Tests/Api.Tests.csproj reference Api/Api.csproj
3.3 Additional Information
To add AutoFixture package to test proj
`dotnet add Api.Tests/Api.Tests.csproj package AutoFixture