I am developing with vscode devcontainers (Ubuntu 22.04).
I have one that I was doing development in (devA), and after team members made changes in the repository, I wanted to pull them in, but not modify devA at all. I created a fresh clone of the repository to build another devcontainer (devB) to see if there were any issues introduced with the code changes.
Directory Structure
desktop/
devA/
.devcontainer/
...
...
new_folder/
devB/
.devcontainer/
...
...
Based on the vscode documentation, it looks like you should be able to open 1 devcontainer per vscode window. Right now, if I start one devcontainer, the other one will disconnect, or vscode will think that they are both the same devcontainer (ie when I open devB, it has uncommitted files from the workspace of devA). What are the settings/metadata that create the identity of the devcontainers and what could I change to have vscode treat them as 2 separate devcontainers?
I have tried changing the workspace name and ‘name’ in devcontainer.json but those did not resolve anything. Below I have my devcontainer.json
{
"name": "Pose Dev Container",
"dockerComposeFile": ["docker-compose.yaml"],
"service": "pose",
"workspaceFolder": "/Pose",
"forwardPorts": [3306],
"postCreateCommand": "pip install ipykernel",
"customizations": {
"settings": {
"python.pythonPath": "/usr/local/bin/python"
},
"vscode": {
"extensions": [
"ms-python.python",
"ms-toolsai.jupyter"
]
}
}
}
2
Answers
I am awarding the bounty to @VonC for the detailed answer, but I also wanted to add an answer to go over the testing I did.
I cloned 2 copies of the same repository and incrementally changed settings until I was able to open both copies independently.
In order I:
Renaming the repo name was the only thing that worked and allowed me to open 2 independent devcontainers of the same repo.
Since VSCode is facing difficulty in treating
devA
anddevB
as two separate devcontainers, one possible way to manage them separately could be to use a more explicit Docker Compose setup to specify the services and dependencies for each devcontainer.That way, you can make sure each devcontainer has its own separate environment, network settings, etc., which can potentially resolve the overlapping workspace issue you are facing.
Start with creating separate
docker-compose.yaml
files fordevA
anddevB
.Make sure they have different service names and network configurations.
And update the
devcontainer.json
files ofdevA
anddevB
to point to their respectivedocker-compose.yaml
files and services.Make sure that the
workspaceFolder
property indevcontainer.json
points to unique locations in each devcontainer.You should get something like:
True: devcontainers are designed to be fully isolated environments, and in theory, it should be possible to open two clones of the same repository in different VS Code windows without interference. That isolation is typically managed by Docker, which creates separate containers for each environment.
But here, the issue is that VS Code is unable to differentiate between two instances that are too similar, especially if they share the same context or identifiers.
To make sure VS Code treats them as separate devcontainers, without altering Docker Compose configurations, you could try and make sure that each devcontainer is opened in a separate VS Code window, and that the context (the root directory opened in VS Code) is set to the respective development directory (
devA
ordevB
).Also, in the
devcontainer.json
for each project, try and specify a unique"containerName"
which is used by Docker to create a distinct container instance.Adjust the workspace settings (
*.code-workspace
) to make sure they have unique names and paths for their workspace storage.Note:
docker ps -a
docker inspect <container_name_or_id>
If volumes are used, make sure that the containers do not mount the same volume as this can cause conflicts and shared state.
Make sure the forwarded ports in the
devcontainer.json
files do not conflict. If they are the same, you may need to change one of them to use a different port.Sometimes simply restarting VS Code or reloading the window (
Developer: Reload Window
command) can help it to recognize the separate containers.Additionally, checking the VS Code’s output panel for logs related to the Remote – Containers extension might provide clues as to why the containers are being conflated.