Connect to multiple containers
Currently you can only connect to one container per Visual Studio Code window. However, you can spin up multiple VS Code windows toattach to them.
If you'd prefer to usedevcontainer.json instead and are using Docker Compose, you can create separatedevcontainer.json files for each service in your source tree, each pointing to a commondocker-compose.yml.
To see how this works, consider this example source tree:
📁 project-root 📁 .git 📁 .devcontainer 📁 python-container 📄 devcontainer.json 📁 node-container 📄 devcontainer.json 📁 python-src 📄 hello.py 📁 node-src 📄 hello.js 📄 docker-compose.ymlThe 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 thedocker-compose.yml in the root is as follows:
services: python-api: image:mcr.microsoft.com/devcontainers/python:1-3.12-bookworm volumes: # Mount the root folder that contains .git -.:/workspace command:sleep infinity # ... node-app: image:mcr.microsoft.com/devcontainers/typescript-node:1-20-bookworm volumes: # Mount the root folder that contains .git -.:/workspace command:sleep infinity # ...You can then set up./devcontainer/python-container/devcontainer.json for Python development as follows:
{ "name":"Python Container", "dockerComposeFile": ["../../docker-compose.yml"], "service":"python-api", "shutdownAction":"none", "workspaceFolder":"/workspace/python-src"}Next, you can set up./devcontainer/node-container/devcontainer.json for Node.js development by changingworkspaceFolder.
{ "name":"Node Container", "dockerComposeFile": ["../../docker-compose.yml"], "service":"node-app", "shutdownAction":"none", "workspaceFolder":"/workspace/node-src"}The"shutdownAction": "none" in thedevcontainer.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.
- RunDev 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 usingFile >New Window.
- Open your project at root level in the current window.
- RunDev 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.
- RunDev 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.
- RunDev 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 toextend your Docker Compose file for development, you should use a singledocker-compose.yml that extendsboth services (as needed) and is referenced inbothdevcontainer.json files.
For example, consider thisdocker-compose.devcontainer.yml file:
services: python-api: volumes: -~:~/local-home-folder # Additional bind mount # ... node-app: volumes: -~/some-folder:~/some-folder # Additional bind mount # ...Both.devcontainer.json files would be updated as follows:
"dockerComposeFile": [ "../../docker-compose.yml", "../../docker-compose.devcontainer.yml",]This list of compose files is used when starting the containers, so referencing different files in eachdevcontainer.json can have unexpected results.