简体   繁体   English

如何在 VSCode 远程开发容器中处理多个“network_mode”?

[英]How to deal with more than one `network_mode` in a VSCode Remote dev container?

I would like to have an application, database and redis service running in a dev container where I'd be able to access my database and redis inside the container, application and on Windows, this is what currently works just as I wanted for my application and database:我想在开发容器中运行一个应用程序、数据库和 redis 服务,我可以在其中访问我的数据库和 redis 在容器、应用程序和 ZAEA23489CE3AA9B6406EBB28E0CDA43Z和数据库:

.devcontainer.json : .devcontainer.json

{
  "name": "Node.js, TypeScript, PostgreSQL & Redis",
  "dockerComposeFile": "docker-compose.yml",
  "service": "akira",
  "workspaceFolder": "/workspace",
  "settings": {
    "typescript.tsdk": "node_modules/typescript/lib",
    "sqltools.connections": [
      {
        "name": "Container database",
        "driver": "PostgreSQL",
        "previewLimit": 50,
        "server": "database",
        "port": 5432,
        "database": "akira",
        "username": "ailuropoda",
        "password": "melanoleuca"
      }
    ],
    "editor.formatOnSave": true,
    "editor.codeActionsOnSave": {
      "source.fixAll": true
    }
  },
  "extensions": [
    "aaron-bond.better-comments",
    "dbaeumer.vscode-eslint",
    "esbenp.prettier-vscode",
    "mtxr.sqltools",
    "mtxr.sqltools-driver-pg",
    "redhat.vscode-yaml"
  ],
  "forwardPorts": [5432],
  "postCreateCommand": "npm install",
  "remoteUser": "node"
}

docker-compose.yml : docker-compose.yml

version: "3.8"

services:
  akira:
    build:
      context: .
      dockerfile: Dockerfile
    command: sleep infinity
    env_file: .env
    volumes:
      - ..:/workspace:cached

  database:
    image: postgres:latest
    restart: unless-stopped
    environment:
      POSTGRES_USER: ailuropoda
      POSTGRES_DB: akira
      POSTGRES_PASSWORD: melanoleuca
    ports:
      - 5432:5432
    volumes:
      - pgdata:/var/lib/postgresql/data

  redis:
    image: redis:alpine
    tty: true
    ports:
      - 6379:6379

volumes:
  pgdata:

Dockerfile : Dockerfile

ARG VARIANT="16-bullseye"
FROM mcr.microsoft.com/vscode/devcontainers/typescript-node:0-${VARIANT}

As you can see I already tried to achieve what I wanted to using networks but without success, my question is: How can I add Redis to my services while still being able to connect redis and database inside the application and on Windows?如您所见,我已经尝试实现我想要使用networks但没有成功,我的问题是:如何将 Redis 添加到我的服务中,同时仍然能够连接 redis 和应用程序内的数据库和 ZAEA23489CE3AA9B640Z3EBB28?

Delete all of the network_mode: settings.删除所有network_mode:设置。 Compose will use the default network_mode: bridge . Compose 将使用默认的network_mode: bridge You'll be able to communicate between containers using their Compose service names as host names, as described in Networking in Compose in the Docker documentation.您将能够使用它们的 Compose 服务名称作为主机名在容器之间进行通信,如 Docker 文档中的 Compose 中的网络中所述。

version: "3.8"
services:
  akira:
    build: .
    env_file: .env
    environment:
      PGHOST: database

  database:
    image: postgres:latest
    ...

In SO questions I frequently see trying to use network_mode: to make other things appear as localhost .在 SO 问题中,我经常看到尝试使用network_mode:使其他内容显示为localhost That host name is incredibly context-sensitive;该主机名具有令人难以置信的上下文相关性; if you asked my laptop, one of the Stack Overflow HTTP servers, your application container, or your database container who localhost is, they'd each independently say "well I am of course" but referring to a different network context.如果您问我的笔记本电脑、Stack Overflow HTTP 服务器之一、您的应用程序容器或localhost是您的数据库容器,他们会各自独立地说“我当然是”,但指的是不同的网络上下文。 network_mode: service:... sounds like you're trying to make the other container be localhost ; network_mode: service:...听起来您正在尝试使另一个容器成为localhost in practice it's extremely unusual to use this.在实践中,使用它是非常不寻常的。

You may need to change your application code to make settings like the database location configurable, depending on where they're running, and environment variables are an easy way to set this in Docker.您可能需要更改应用程序代码以使数据库位置等设置可配置,具体取决于它们运行的位置,并且环境变量是在 Docker 中设置此设置的简单方法。 For this particular example I've used the $PGHOST variable the standard PostgreSQL client libraries use;对于这个特定示例,我使用了标准 PostgreSQL 客户端库使用的$PGHOST变量; in a Typescript/Node context you may need to change your code to refer to process.env.SOME_HOSTNAME instead of 'localhost' .在 Typescript/Node 上下文中,您可能需要更改代码以引用process.env.SOME_HOSTNAME而不是'localhost'

If you're using Docker on WSL, I found that I can often not connect when the process is listening on::1, but when explicitly binding the port to 127.0.0.1 makes the service accessible through Windows.如果您在 WSL 上使用 Docker,我发现当进程正在侦听::1 时我经常无法连接,但是当显式将端口绑定到 127.0.0.1 时,可以通过 Windows 访问服务。

So something like所以像

ports:
  - 127.0.0.1:5432:5432

might work可能有用

Switch all non-dev containers to network_mode: service:akira将所有非开发容器切换到 network_mode: service:akira

version: '3.8'
services:
  app:
    build:
      context: .
      dockerfile: Dockerfile
    volumes:
      - ../..:/workspace:cached
    command: sleep infinity
  postgresql:
    image: postgres:14.1
    network_mode: service:akira
    restart: unless-stopped
    volumes:
      - ../docker/volumes/postgresql:/var/lib/postgresql/data
    environment:
      POSTGRES_PASSWORD: postgres
      POSTGRES_USER: postgres
      POSTGRES_DB: pornapp
  redis:
    image: redis
    network_mode: service:akira
    restart: unless-stopped
    volumes:
      - ../docker/volumes/redis:/data

It seems this was the original configuration: https://github.com/microsoft/vscode-dev-containers/pull/523看来这是原始配置: https://github.com/microsoft/vscode-dev-containers/pull/523

But it was reverted back because if you rebuild the dev container while others servies are running, the port forwarding will break: https://github.com/microsoft/vscode-dev-containers/issues/537但它被恢复了,因为如果您在其他服务运行时重建开发容器,端口转发将中断: https://github.com/microsoft/vscode-dev-containers/issues/537

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法通过“network_mode:host”访问容器 - Cannot access container by “network_mode: host” 如何从该模式之外的另一个容器获取特定 network_mode 中容器的 IP? - How can I get the IP of a container in a specific network_mode from another container outside of that mode? 如果使用“network_mode:host”,容器无法访问主机服务 - container not able to access host service if “network_mode: host” used 使用network_mode时如何访问在容器中运行的网站:主机 - How to access a website running in a container when you´re using network_mode: host Ansible `docker_container` 选项 `network_mode` 和 `networks` - 它们相交吗? - Ansible `docker_container` options `network_mode` and `networks` - are they intersecting? 无法到达具有Network_Mode主机的Tomcat Docker容器 - Tomcat Docker Container With Network_Mode Host Can't Be Reached Kafka Docker network_mode - Kafka Docker network_mode 使用network_mode:“主机”公开由docker组成的容器,并使容器与容器联网 - Expose a docker composed container with network_mode: “host” and keep container to container networking go delve 远程调试不适用于 docker "network_mode: host" - go delve remote debugging does not work with docker "network_mode: host" 当network_mode:“bridge”(docker-compose)时,无法从localhost连接容器端口 - Unable to connect container port from localhost when network_mode: “bridge” (docker-compose)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM