Devcontainer is an open specification for enriching containers with development-specific content and settings. VSCode and Github Codespaces currently have full support for it. You can find the official documentation here. It's a great way to ensure consistency across different environments and to make sure that your development environment is isolated from your host machine. It's also a great way to ensure that your development environment is reproducible.
With devcontainer, you can easily share your development environment with your team, and you can also use it to develop on the go - using Github Codespaces or your remote docker context running on a Cloud VM, a Raspberry Pi, or your local machine (even if you don't have a public IP, more on that later)
Let's first have a quick look at what a devcontainer is - by creating one. We'll create a simple devcontainer with Ubuntu. We'll use the official vscode devcontainer tutorial as a reference.
devcontainer-tutorial and open it in vscode.
Ctrl+Shift+P to open the command palette, and select
Dev Containers: Add Development Container Configuration Files...
Ubuntu from the list of available configuration templates. Don't add any additional features for now. Don't worry, you can add them later. You should see a new directory named
.devcontainer with a
devcontainer.json file inside it.
open folder in container button in the bottom right corner of your vscode window. Click on it. If not, press
Ctrl+Shift+P and select
Dev Containers: Reopen in Container. It'll take a while to build the container for the first time.
ls to see the contents of the current directory. You should see the same files as your host machine.
VSCode created a docker container with Ubuntu and mounted the current directory as a volume inside the container. It also installed some vscode extensions inside the container.
Now, as you have a terminal inside the container, you can run any command inside the container. You can also install any package inside the container. But remember, the container is ephemeral. If you exit the container, and reopen it, you'll have to install the packages again.
For a more persistent solution, you can create a
Dockerfile inside the
.devcontainer folder as described here. You can also use a
docker-compose.yml file to create a multi-container devcontainer as described here