27

I have a project that creates multiple Docker images. I would like to setup a devcontainer in vscode-remote for each image so that I may spin up a container for each image.

I only need to spin up and connect to one container/image at a time.

Is it supported to have multiple devcontainer files? Maybe something like .devcontainer/devcontainer1.json and .devcontainer/devcontainer2.json (supposing I only had two images that I wanted to use)?

Each devcontainer would use an image with a different name and also requires different runArgs.

Bidski
  • 435
  • 1
  • 4
  • 6

3 Answers3

14

Just found this here. I checked the documentation because I was interested in this as well. And the documentation states the following

In some cases, a single container environment isn't sufficient. Fortunately, Remote - Containers supports Docker Compose managed multi-container configurations.

You can either:

  1. Work with a service defined in an existing, unmodified docker-compose.yml.
  2. Create a new docker-compose.yml (or make a copy of an existing one) that you use to develop a service.
  3. Extend your existing Docker Compose configuration to develop the service.
  4. Use separate VS Code windows to work with multiple Docker Compose-defined services at once.

https://code.visualstudio.com/docs/remote/containers#_using-docker-compose

Maybe you want to check this.

Documentation for your use case can be found under the following page:

https://code.visualstudio.com/docs/remote/containers-advanced#_connecting-to-multiple-containers-at-once

You do this by using docker-compose.yml and need to have a folder structure like the following:

enter image description here

Community
  • 1
  • 1
  • 2
    Does this allow you to have `project-root` as the source folder in all containers or is each container restricted to their respective folders? – Bidski May 13 '20 at 03:40
  • 2
    I am currently working on it. My current status is, that VS code window gets opened on the subfolder, but I map it to the workspace I can open VSCode for the whole project root manually. I will update my answer with my docker-compose and devcontainer.json as soon as I get something working in basic. And FYI: It is a bit problematic to use windows and Unix based images at the same time – Sebastian Schütze May 13 '20 at 06:33
  • Any updates on project-root? – kuga Sep 22 '21 at 10:30
  • I haven't done or researched anything about it since then. – Sebastian Schütze Oct 20 '21 at 13:44
  • @Bidski in order to set `project-root` as the workspace root include a `"workspaceFolder"` entry in your `.devcontainer.json` and point it at the location `project-root` on the container - e.g. `"workspaceFolder": "/path/to/project-root"`. Then, even if you "Open Folder in Container..." with `container1-src` VSCode will open with `project-root` as the workspace – stav May 25 '22 at 21:25
7

I found the other answers too cumbersome as they only work with F1 -> Dev Containers: Open Folder in Container and discovered that (with VS Code v1.77) if you place your devcontainer.json files in subfolders of .devcontainer it even works with F1 -> Dev Containers: Reopen in Container offering you a selection of all your services rightaway without the need to dig through your folder structure.

screenshot VS Code devcontainer selection

thomiel
  • 2,467
  • 22
  • 37
1

You should create another folder devcontainer2/.devcontainer and create devcontainer.json under that. Also create another dockerfile for 2nd container as container2.Dockerfile in your project root.

In your devcontainer.json file set the context and dockerFile path correctly. Also if you would like to change workspacefolder you can change it. Refer the below devcontainer.json

    {
        "name": "Container2",
    
        // Sets the run context to 2 level up instead of the devcontainer2/.devcontainer folder.
        "context": "../..",
    
        // use the second container file.
        "dockerFile": "../../container2.Dockerfile",
        
        // When dev container is opened, below folder will be in workspace
        "workspaceFolder": "/workspaces/project"
    }

This link will give you some idea. It talks about how to setup devcontainers in Monorepo. https://code.visualstudio.com/remote/advancedcontainers/change-default-source-mount

MatthewMartin
  • 32,326
  • 33
  • 105
  • 164
Samselvaprabu
  • 16,830
  • 32
  • 144
  • 230
  • I've set this up but what command do I execute to build one of these subfolder devcontainers and attach to it? It seems it's planned but there's no way to actually do it yet... am I missing something? https://github.com/microsoft/vscode-remote-release/issues/7548 – Kevin Pauli Jan 10 '23 at 16:50