添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
风流的乌冬面  ·  Previously working, ...·  39 分钟前    · 
霸气的泡面  ·  Connect to multiple ...·  2 小时前    · 
飞翔的开心果  ·  mongoose.connection ...·  2 天前    · 
风流倜傥的炒粉  ·  How to Fix the ...·  2 天前    · 
玩篮球的煎鸡蛋  ·  Connect Power BI to ...·  1 周前    · 
高大的感冒药  ·  Unity/docs/UnityAssert ...·  4 周前    · 
刚分手的小刀  ·  Amazon.com·  3 月前    · 
耍酷的红烧肉  ·  合肥市轨道交通建设·  5 月前    · 

Connect to multiple containers

Currently you can only connect to one container per Visual Studio Code window. However, you can spin up multiple VS Code windows to attach to them .

If you'd prefer to use devcontainer.json instead and are using Docker Compose, you can create separate devcontainer.json files for each service in your source tree, each pointing to a common docker-compose.yml .

To see how this works, consider this example source tree:

📁 project-root
    📁 .git
    📁 .devcontainer
      📁 python-container
        📄 devcontainer.json
      📁 node-container
        📄 devcontainer.json
    📁 python-src
        📄 hello.py
    📁 node-src
        📄 hello.js
    📄 docker-compose.yml

The location of the .git folder is important, since we will need to ensure the containers can see this path for source control to work properly.

Next, assume the docker-compose.yml in the root is as follows:

version: '3'
services:
  python-api:
    image: mcr.microsoft.com/devcontainers/python:1-3.12-bookworm
    volumes:
      # Mount the root folder that contains .git
      - .:/workspace:cached
    command: sleep infinity
    links:
      - node-app
    # ...
  node-app:
    image: mcr.microsoft.com/devcontainers/typescript-node:1-20-bookworm
    volumes:
      # Mount the root folder that contains .git
      - .:/workspace:cached
    command: sleep infinity
    # ...

You can then set up ./devcontainer/python-container/devcontainer.json for Python development as follows:

"name": "Python Container", "dockerComposeFile": ["../../docker-compose.yml"], "service": "python-api", "shutdownAction": "none", "workspaceFolder": "/workspace/python-src"

Next, you can set up ./devcontainer/node-container/devcontainer.json for Node.js development by changing workspaceFolder.

"name": "Node Container", "dockerComposeFile": ["../../docker-compose.yml"], "service": "node-app", "shutdownAction": "none", "workspaceFolder": "/workspace/node-src"

The "shutdownAction":"none" in the devcontainer.json files is optional, but will leave the containers running when VS Code closes -- which prevents you from accidentally shutting down both containers by closing one window.

Connect to multiple containers in multiple VS Code windows

  • Open a VS Code window at the root level of the project.
  • Run Dev Containers: Reopen in Container from the Command Palette (F1) and select Python Container.
  • VS Code will then start up both containers, reload the current window and connect to the selected container.
  • Next, open a new window using File > New Window.
  • Open your project at root level in the current window.
  • Run Dev Containers: Reopen in Container from the Command Palette (F1) and select Node Container.
  • The current VS Code window will reload and connect to the selected container.
  • You can now interact with both containers from separate windows.

    Connect to multiple containers in a single VS Code window

  • Open a VS Code window at the root level of the project.
  • Run Dev Containers: Reopen in Container from the Command Palette (F1) and select Python Container.
  • VS Code will then start up both containers, reload the current window and connect to the selected container.
  • Run Dev Containers: Switch Container from the Command Palette (F1) and select Node Container.
  • The current VS Code window will reload and connect to the selected container.
  • You can switch back with the same command.
  • Extending a Docker Compose file when connecting to two containers

    If you want to extend your Docker Compose file for development, you should use a single docker-compose.yml that extends both services (as needed) and is referenced in both devcontainer.json files.

    For example, consider this docker-compose.devcontainer.yml file:

    version: '3'
    services:
      python-api:
        volumes:
          - ~:~/local-home-folder:cached # Additional bind mount
        # ...
      node-app:
        volumes:
          - ~/some-folder:~/some-folder:cached # Additional bind mount
        # ...
    

    Both .devcontainer.json files would be updated as follows:

    "dockerComposeFile": [
      "../../docker-compose.yml",
      "../../docker-compose.devcontainer.yml",
    

    This list of compose files is used when starting the containers, so referencing different files in each devcontainer.json can have unexpected results.

  • Connect to multiple containers in multiple VS Code windows
  • Connect to multiple containers in a single VS Code window
  • Extending a Docker Compose file when connecting to two containers
  •