Search code examples
dockernetwork-programmingvisual-studio-codecontainers

Is there a way for two containers in VSCode to communicate from different projects?


I have two different Node.JS Projects I created in VSCode. On occasion, project A needs to make a call into project B. Project A is running on port 60100. Project B is running on port 60200. When i try to call Project B using http://localhost:60200/ I get a transport error. If i expose port 60200 on both container configs (devcontainer.json) it will throw an error because the port is already in use.

I know i could use a docker-compose and run them in the same project, but they have separate git homes and are standalone most of the time.

Is there anything I can do to make them connected? Maybe use a docker-compose for each separate, but use the same network name in the compose? Would that let them communicate?


Solution

  • Here's what I do (although I use it to get my devcontainer to join a docker-compose network, but the same principles should apply)...

    In my devcontainer.json...

        "initializeCommand": "docker network inspect my_shared_network > /dev/null || docker network create my_shared_network --attachable",
    
        "runArgs": [
            "--network=my_shared_network",
        ],
    

    This creates a named network if it's not there already, and instructs VSCode to use it for the devcontainer.

    You may want to add, e.g., "--hostname=devcontainer-project-b" to the runArgs so that you can use that name in your URL.