overload or extend a Dev Container configuration file (`devcontainer.json`)

247 views Asked by At

I am looking for a possibility to extend or overload a devcontainer.json file that has been added to the repository / version control.

current

Example project structure (can be replicated with the "Try a Dev Container -> Python3" command in vscode):

.
├── .devcontainer
│   └── devcontainer.json
├── .git
│   ├── FETCH_HEAD
...
├── app.py
├── requirements.txt
...

Now the actual devcontainer.json from that example looks different but to simplify, let's assume it looks like this:

{
    "name": "Python 3",
    "image": "mcr.microsoft.com/devcontainers/python:0-3.11",
    "postCreateCommand": "pip3 install -r requirements.txt"
}

This is fine so far.

Attempt

I would like to keep that configuration file. But for some dev PCs that we use, we also have GPUs available. Now when I add

"runArgs": ["--gpus", "all"],

for a PC that actually has a GPU installed, everything is fine.

But for PCs that have not, an error occurs (do not remember correctly, something about nvidia containers).

Goal / Idea

I am looking for something like a configuration that I can keep in the repository, say the above devcontainer.json file. And something like an extending file, that is additionally considered, say devcontainer.extend.json.

In the end, I would add the devcontainer.extend.json to the .gitignore so that personal, machine-dependent configuration like the "runArgs": ["--gpus", "all"] would be placed in that file.

So the devcontainer.extend.json file would contain nothing else than

"runArgs": ["--gpus", "all"],

which would be laod after the original configuration, so that it overloads the original config (in this case not exactly overloading but extending the configuration by adding another setting)

And to be complete, the full devcontainer configuration, when both files are combined, would look similar to this:

{
    "name": "Python 3",
    "image": "mcr.microsoft.com/devcontainers/python:0-3.11",
    "postCreateCommand": "pip3 install -r requirements.txt",
    "runArgs": ["--gpus", "all"]
}

and the new folder structure containing the extending file could be something like this:

.
├── .devcontainer
│   ├── devcontainer.json
│   └── devcontainer.extend.json
├── .git
│   ├── FETCH_HEAD
...
├── app.py
├── requirements.txt
...

Question

Is there something already available, that I am not aware of? Is it even a good strategy to achieve additional configurations?

1

There are 1 answers

0
Pascal On

Overall

After (another) search and also with the hint in the comments I found that there are quite some people requesting this.

For example:

  • Overlay .devcontainer #3279
  • Addition of "extends" top level property to enable simple configuration inheritance #22

To the specific problem

Actually I was able to help myself another way. Opened a feature request on this and got a very handy answer on the very problem with the deviating GPU config for some clients (not the overall container configuration problem).

The trick for the optional GPU configuration was to add

"hostRequirements": {"gpu": "optional"}

to the devcontainer.json