Dev Containers
This guide provides common patterns for using Dev Containers in VS Code. Dev Containers are a way to define a development environment in a container that is isolated from your local machine. This allows you to have a consistent development environment across different machines and operating systems. Dev Containers are defined using a devcontainer.json file that specifies the Docker image to use, the extensions to install, and other settings. This guide will cover how to create a Dev Container, how to use it in VS Code, and common patterns for using Dev Containers in your development workflow.
Common Patterns
Creating a Dev Container
There are two ways to create a Dev Container in VS Code:
Manually creating a devcontainer.json file
You can create a devcontainer.json file manually by adding it to your project folder.
See the Reference for the file format and available fields.
Using this way you need to manually define the template and features.
Using VS Code
- Open the command palette in Visual Studio Code by pressing
Ctrl+Shift+PorCmd+Shift+Pon MacOS. - Type
Dev Container: Add Dev Container Configuration Files...and select it.- Hint the search is fuzzy, so you can type
dev addand it should show up.
- Hint the search is fuzzy, so you can type
- When selecting the template, choose
Ubuntufor example, with the default settings. - Select the features you need, for example
Python, and pressOK- You can select
Keep Defaultsin don’t need a certain version or extra features & tools.
- You can select
- Done!
You should now see a .devcontainer folder in your project with a devcontainer.json file.
// For format details, see https://aka.ms/devcontainer.json. For config options, see the// README at: https://github.com/devcontainers/templates/tree/main/src/ubuntu{ "name": "Ubuntu", // Or use a Dockerfile or Docker Compose file. More info: https://containers.dev/guide/dockerfile "image": "mcr.microsoft.com/devcontainers/base:jammy", "features": { "ghcr.io/devcontainers/features/python:1": {} }
// Features to add to the dev container. More info: https://containers.dev/features. // "features": {},
// Use 'forwardPorts' to make a list of ports inside the container available locally. // "forwardPorts": [],
// Use 'postCreateCommand' to run commands after the container is created. // "postCreateCommand": "uname -a",
// Configure tool-specific properties. // "customizations": {},
// Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root. // "remoteUser": "root"}Opening the current project in a DevContainer
When opening a project which has a devcontainer.json file, you will be prompted to open the project in a DevContainer. If not, you can open the command palette and type Dev Container: Reopen in Container.
Adding features to the DevContainer
There are again two ways to add features to the DevContainer, via the devcontainer.json file or using the command palette.
Add features to the devcontainer.json file to install additional tools and extensions in the DevContainer.
{ "name": "Ubuntu", "image": "mcr.microsoft.com/devcontainers/base:jammy", "features": { "ghcr.io/devcontainers/features/python:1": {}, "ghcr.io/devcontainers/features/node:1": {} }}Or via the command palette: Dev Container: Configure Container Features... and select the features you need.
You should be prompted to rebuild the DevContainer after adding features, if not rebuild the DevContainer.
Rebuilding the DevContainer
If you need to rebuild the DevContainer, you can open the command palette and type Dev Container: Rebuild Container or a similar option.