Post

Set Up VS Code Dev Containers on WSL 2

Use Docker Desktop, WSL 2, and VS Code Dev Containers with project files stored in the Linux filesystem.

Set Up VS Code Dev Containers on WSL 2

A VS Code Dev Container gives a project a repeatable development environment. On Windows, use WSL 2 and keep the repository in the Linux filesystem for better Docker performance.

Prerequisites

Install WSL 2, Docker Desktop with its WSL 2 backend enabled, Visual Studio Code, and the VS Code Dev Containers extension.

In Docker Desktop, check Settings > Resources > WSL integration and enable integration for the distribution that holds your project.

Store the Repository Inside WSL

1
2
3
4
5
mkdir -p ~/projects
cd ~/projects
git clone https://github.com/your-org/your-repo.git
cd your-repo
code .

Prefer /home/yourname/projects/ over a path under C:\Users. Docker accesses files in the WSL filesystem through native Linux I/O, which is especially important for builds and file watchers.

Add a Python Dev Container

Create .devcontainer/devcontainer.json:

1
2
3
4
5
6
7
8
9
10
{
  "name": "Python",
  "image": "mcr.microsoft.com/devcontainers/python:1-3.12-bookworm",
  "postCreateCommand": "python -m pip install --upgrade pip",
  "customizations": {
    "vscode": {
      "extensions": ["ms-python.python"]
    }
  }
}

In VS Code, press F1 and run Dev Containers: Reopen in Container. Inside the container terminal, run python --version and pwd.

Troubleshooting

  • If Docker Desktop does not expose your distribution, enable WSL integration.
  • If file watching is slow, move the repository from C:\ into WSL.
  • If VS Code cannot connect, confirm Docker Desktop is running.

References

This post is licensed under CC BY 4.0 by the author.