Currently you can only connect to one container per Visual Studio Code window. However, you can spin up multiple VS Code windows to
attach to them
.
If you'd prefer to use
devcontainer.json
instead and are using Docker Compose, you can create separate
devcontainer.json
files for each service in your source tree, each pointing to a common
docker-compose.yml
.
To see how this works, consider this example source tree:
The location of the .git folder is important, since we will need to ensure the containers can see this path for source control to work properly.
Next, assume the docker-compose.yml in the root is as follows:
version: '3'services:python-api:image: mcr.microsoft.com/devcontainers/python:1-3.12-bookwormvolumes:# Mount the root folder that contains .git - .:/workspace:cachedcommand: sleep infinitylinks: - node-app# ...node-app:image: mcr.microsoft.com/devcontainers/typescript-node:1-20-bookwormvolumes:# Mount the root folder that contains .git - .:/workspace:cachedcommand: sleep infinity# ...
You can then set up ./devcontainer/python-container/devcontainer.json for Python development as follows:
The "shutdownAction":"none" in the devcontainer.json files is optional, but will leave the containers running when VS Code closes -- which prevents you from accidentally shutting down both containers by closing one window.
Connect to multiple containers in multiple VS Code windows
Open a VS Code window at the root level of the project.
Run Dev Containers: Reopen in Container from the Command Palette (F1) and select Python Container.
VS Code will then start up both containers, reload the current window and connect to the selected container.
Next, open a new window using File > New Window.
Open your project at root level in the current window.
Run Dev Containers: Reopen in Container from the Command Palette (F1) and select Node Container.
The current VS Code window will reload and connect to the selected container.
You can now interact with both containers from separate windows.
Connect to multiple containers in a single VS Code window
Open a VS Code window at the root level of the project.
Run Dev Containers: Reopen in Container from the Command Palette (F1) and select Python Container.
VS Code will then start up both containers, reload the current window and connect to the selected container.
Run Dev Containers: Switch Container from the Command Palette (F1) and select Node Container.
The current VS Code window will reload and connect to the selected container.
You can switch back with the same command.
Extending a Docker Compose file when connecting to two containers
If you want to extend your Docker Compose file for development, you should use a single docker-compose.yml that extends both services (as needed) and is referenced in bothdevcontainer.json files.
For example, consider this docker-compose.devcontainer.yml file: