Incorrect field used as mapping key for deserialization of 'mounts' in devcontainer.json
If specifying mounts in devcontainer.json and trying to mount the same volume in different locations, you get a deserialization error.
[2024-05-08T20:39:45.846Z] parsing /tmp/devcontainercli-root/docker-compose/docker-compose.devcontainer.containerFeatures-1715200785780-516d96c0-3c67-479f-b191-c3c3d323c3c3.yml: yaml: unmarshal errors:
line 28: mapping key "build-obj" already defined at line 26
However this works without problems if you specify the volumes in docker-compose.yml.
volumes:
- build-obj:/workspace/src/a/build
- build-obj:/workspace/src/b/build
If a unique key is needed, the target and not the source of the mount should be used.
Log fragment
[2024-05-08T20:39:45.780Z] Docker Compose override file for creating container:
version: '3.8'
services:
'dev-build':
entrypoint: ["/bin/sh", "-c", "echo Container started\n
trap \"exit 0\" 15\n
\n
exec \"$$@\"\n
while sleep 1 & wait $$!; do :; done", "-"]
command: ["/usr/bin/sleep","infinity"]
labels:
- 'devcontainer.local_folder=/root/workspace/dev-container'
- 'devcontainer.config_file=/root/workspace/dev-container/.devcontainer/devcontainer.json'
volumes:
- /root/workspace/dev-container:/workspace
- build-obj:/workspace/src/a/build
- build-obj:/workspace/src/b/build
- vscode:/vscode
volumes:
build-obj:
build-obj:
vscode:
external: true
[2024-05-08T20:39:45.780Z] Writing docker-compose.devcontainer.containerFeatures-1715200785780-516d96c0-3c67-479f-b191-c3c3d323c3c3.yml to /tmp/devcontainercli-root/docker-compose
[2024-05-08T20:39:45.781Z] Start: Run: /usr/bin/docker-compose --project-name devcontainer -f /root/workspace/dev-container/.devcontainer/docker-compose.yml -f /tmp/devcontainercli-root/docker-compose/docker-compose.devcontainer.build-1715200784920.yml -f /tmp/devcontainercli-root/docker-compose/docker-compose.devcontainer.containerFeatures-1715200785780-516d96c0-3c67-479f-b191-c3c3d323c3c3.yml up -d
[2024-05-08T20:39:45.846Z] parsing /tmp/devcontainercli-root/docker-compose/docker-compose.devcontainer.containerFeatures-1715200785780-516d96c0-3c67-479f-b191-c3c3d323c3c3.yml: yaml: unmarshal errors:
line 28: mapping key "build-obj" already defined at line 26
Hi 👋
The error you're seeing is due to the way the devcontainer.json file is being converted to a Docker Compose file. When you specify the same volume multiple times in the mounts field of devcontainer.json, it's being converted to a Docker Compose file with duplicate volume definitions, which is not valid.
In Docker Compose, you can mount the same volume to multiple locations in a container, as you've shown in your docker-compose.yml example. However, when specifying mounts in devcontainer.json, each mount needs to have a unique source.
A workaround for this issue is to define the mounts directly in a docker-compose.yml file instead of devcontainer.json. You can then reference this Docker Compose file in your devcontainer.json:
{
"dockerComposeFile": "./docker-compose.yml",
"service": "your-service-name",
"workspaceFolder": "/workspace"
}
In your docker-compose.yml file, you can then define the mounts as you've shown:
services:
your-service-name:
volumes:
- build-obj:/workspace/src/a/build
- build-obj:/workspace/src/b/build
This way, you can mount the same volume to multiple locations in your container.
// cc @chrmarti Feel free to correct if I have misunderstood the code-flow! 👀