This is a convenient function to create isolated, self-contained environment for development.
For example, I want to test my Go/Python app in different versions w/wo external service dependencies. Previsouly I need to set up virtual env for Python or managing multiple versions for Go. Now by taking the advantage of dev container I can easily develop code inside the full-feature container(or docker compose) with SVC editor even in parallel, the setup is as simple as below.
Prerequisite
The dev container overview.
- Docker installed at target machine(local or remote)
- VSC Dev Container extension installed
Setup
How to Create a Dev Container
- create a
.devcontainer
folder in your project root directory. - create a
devcontainer.json
(not hidden) file inside.devcontainer
folder and add settings, for example:
1 | // For format details, see https://aka.ms/devcontainer.json. For config options, see the |
- start dev container:
1 | Dev Containers: Reopen in Container |
- if any change is made in
.devcontainer.json
, rebuild it:
1 | Dev Containers: Rebuild Containers |
How to Attach to a Running Container
Visual Studio Code can create and start containers for you but that may not match your workflow and you may prefer to “attach” VS Code to a running Docker container - regardless of how it was started.
Once attached, you can install extensions, edit, and debug like you can when you
open a folder in a container using devcontainer.json
.
This is useful to modify and test a running docker container on the fly without codebase on your local.
- attach VSC to a running container
1 | Dev Containers: Attach to Running Container |
If you need to repeatedly attach to a running container, you can set up configuration in image or container-name level, so next time the attach will start in the right step and place.
Templates
There is a bunch of exisitng dev container template to pick up, please check:
1 | Dev Containers: Add Dev Container Configuration Files |
For example, Go & PostgreSQL Docker compose dev container setup.
Other Use Cases
1.Open a folder on a remote SSH host in a container
- install Docker on remote host.
- ssh to the remote host and open the target folder by VSC.
- create and set up the dev container config file.
- start the remote dev container from VSC.