Remote - Containers configure multiple services in devcontainer.json
I am developing using docker-compose with containers including go and node containers.
I would like to develop vscode attached separately to go container and node container respectively, but it seems that only one service can be specified in the current devcontainer.json specification.
Please let me know if there is a way to attach to multiple containers.
@kan We don't support this from a single VS Code window right now, but you can do this using multiple VS Code windows using the extension's "attach" functionality. Here's how:
- Open your folder in a container
- After everything is up, File > New Window
- F1 > Remote-Containers: Attach to Running Container...
- Select your other container from the list
Each Window should now be connected to a different container. You can also add "shutdownAction": "none" to devcontainer.json to keep the containers running when you shut down VS Code.
@Chuxel thank you. I understand that I can attach to multiple containers in the way you indicate.
In that case, for example, what will be the handling of the extension executed in the container? I would like to use the eslint extension in the container of node, but how should I describe the case of using vscode-go in the container of go in devcontainer.json ?
@kan There's two ways you can do this -- and there's also a way to do this without attaching and just opening a folder in a container twice.
If you attach:
- Once you've connected to the container, you can install any extension via the extensions panel and they'll install inside the container.
- You could also drop a
.vscode/extensions.jsonfile in the folder for the other container with recommended extensions so you'll be prompted to install them.
The other way is to use two devcontainer.json files from different folders that point to a common docker-compose.yml. VS Code will actually attach to running containers if they are already up when you open a folder in a container -- which is why this works. I'm actually curious if this alternative would work for you given your source code setup.
FYI @chrmarti @egamma - Wonder if we should doc this.
Here's a super simplified view of this alternate setup. Say you've got your folders setup like this:
📁container1-src
📄.devcontainer.json
📄hello.go
📁container2-src
📄.devcontainer.json
📄hello.js
📄docker-compose.yml
Next, assume this is your docker-compose.yml in the root:
version: '3'
services:
container-1:
image: ubuntu:bionic
volumes:
- ./container-1-src:/workspace
- ~/.gitconfig:/root/.gitconfig
command: sleep infinity
links:
- container-2
container-2:
image: ubuntu:bionic
volumes:
- ./container-2-src:/workspace
- ~/.gitconfig:/root/.gitconfig
command: sleep infinity
Your container1-src/.devcontainer.json looks like this:
{
"name": "Container 1",
"dockerComposeFile": ["../docker-compose.yml"],
"service": "container-1",
"workspaceFolder": "/workspace",
"extensions": ["ms-vscode.Go"],
"shutdownAction": "none"
}
And container2-src/.devcontainer.json looks like this:
{
"name": "Container 2",
"dockerComposeFile": ["../docker-compose.yml"],
"service": "container-2",
"workspaceFolder": "/workspace",
"extensions": ["dbaeumer.vscode-eslint"],
"shutdownAction": "none"
}
To connect to both:
- F1 > Open Folder in Container... >
container1-srcfolder - This will start up both containers and connect this window to service container-1 and install the Go extension.
- File > New Window
- In the new Window, F1 > Open Folder in Container... >
container2-srcfolder - This will connect to the already running container-2 and install ESLint.
The "shutdownAction":"none" in the devcontainer.json files will leave the containers running even if you close the two VS Code windows -- which prevents you from accidentally shutting down both containers (and thus both windows stop working) by closing one. This part is optional.
Here's the test project: multi-container.zip
@Chuxel Thank you for the explanation. I will try to find out how to prepare devcontainer.json for each container.
@Chuxel Sounds good to me. Maybe we can have this as a separate page under 'advanced' topics. It will be good to collect more feedback from developers as we haven't used this setup ourselves much.
👍 on documenting this as an advanced setup.