Opening a second vscode devcontainer closes the first

267 views Asked by At

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"
        ]
    }

} }

3

There are 3 answers

0
Kunal Shah On BEST ANSWER

I 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.

desktop/                    
  |                         
  |--cloneA/
  |   |--myrepo/       
  |   |   |--.devcontainer/     
  |   |     |--devcontainer.json
  |   |   |--docker-compose.yaml 
  |   |   |--...                             
  |--cloneB/
  |   |--myrepo/       
  |   |   |--.devcontainer/     
  |   |     |--devcontainer.json
  |   |   |--docker-compose.yaml 
  |   |   |--...    

In order I:

  • Modified one devcontainer so different ports were exposed
  • Modified "name" in .devcontainer.json for one devcontainer
  • Modified the "workspaceFolder" name for one devcontainer
  • Modified the image name in the docker-compose file for one devcontainer
  • Modified cloneB/myrepo to be named cloneB/myrepo_test

Only after renaming the repo name was I able to open 2 independent devcontainers of the same repo.

2
VonC On

Since VSCode is facing difficulty in treating devA and devB 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 for devA and devB.
Make sure they have different service names and network configurations.

// For devA:
{
  "name": "DevA Container",
  "dockerComposeFile": ["../docker-compose.yaml"],
  "service": "devA_service",
  "workspaceFolder": "/workspace/devA",
  "forwardPorts": [3306],
  "postCreateCommand": "pip install ipykernel",
  "customizations": {
     "settings": {
        "python.pythonPath": "/usr/local/bin/python"
     },
     "vscode": {
        "extensions": [
           "ms-python.python",
           "ms-toolsai.jupyter"
        ]
     }
  }
}

// For devB:
{
  "name": "DevB Container",
  "dockerComposeFile": ["../docker-compose.yaml"],
  "service": "devB_service",
  "workspaceFolder": "/workspace/devB",
  "forwardPorts": [3306],
  "postCreateCommand": "pip install ipykernel",
  "customizations": {
     "settings": {
        "python.pythonPath": "/usr/local/bin/python"
     },
     "vscode": {
        "extensions": [
           "ms-python.python",
           "ms-toolsai.jupyter"
        ]
     }
  }
}

And update the devcontainer.json files of devA and devB to point to their respective docker-compose.yaml files and services.
Make sure that the workspaceFolder property in devcontainer.json points to unique locations in each devcontainer.

// devA/.devcontainer/devcontainer.json
{
  "name": "DevA Container",
  "dockerComposeFile": ["../docker-compose.yaml"],
  "service": "devA_service",
  "workspaceFolder": "/workspace/devA",
  // rest of the settings 
}

// new_folder/devB/.devcontainer/devcontainer.json
{
  "name": "DevB Container",
  "dockerComposeFile": ["../docker-compose.yaml"],
  "service": "devB_service",
  "workspaceFolder": "/workspace/devB",
  // rest of the settings
}

You should get something like:

desktop/                    
  |                         
  |--devA/                 
  |   |--.devcontainer/     
  |   |  |--devcontainer.json
  |   |--docker-compose.yaml 
  |   |--...                 
  |--new_folder/            
      |--devB/               
          |--.devcontainer/  
          |  |--devcontainer.json
          |--docker-compose.yaml
          |--...

I thought that a benefit for devcontainers was that they are fully contained.

I would think that I should be able to open 2 clones of the same repository in different windows and have them be fully independent without having to create/modify docker-compose file.

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 or devB).

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.

// devA/.devcontainer/devcontainer.json
{
  "name": "DevA Container",
  "containerName": "devA-container",
  ...
}

// devB/.devcontainer/devcontainer.json
{
  "name": "DevB Container",
  "containerName": "devB-container",
  ...
}

Adjust the workspace settings (*.code-workspace) to make sure they have unique names and paths for their workspace storage.

Note:

  • To list all containers: docker ps -a
  • To inspect a specific container: 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.

0
coderoo On

I'm facing the same problem and I think I've found this workaround:

When first cloning the repo, use the option: "Clone Repository in Named Container Volume" instead of "Clone Repository in Container Volume".

Then select "Create a new volume". Give a name like workspace-1.

It will then ask "Enter the target folder name". Give the name of the repo.

It will then form the dev container.

When you want a separate workspace, re-do the above steps but this time, specify workspace-2.

This seems to solve the issue and I get two independent workspaces. I'm not sure though, if this is the official procedure.

Note

I've noticed that if you clone another (unrelated) repo into the same workspace, then its baseline OS image (Ubuntu for example) should match the first repo's. Otherwise the second REPO's OS seems to override first repo's. Here's what I mean:

└── workspace-1
    ├── cpp-app-repo
    │   ├── src.cpp
    │   ├── .devcontainer
    │   └── README.md
    └── py-app-repo
        ├── src.py
        ├── .devcontainer
        └── README.md

It seems both cpp-app-repo and py-app-repo should have the same baseline OS image (e.g. Ubuntu).

Again, not sure what the official guidelines are here.