Docker-从容器组成接入局域网

Docker-compose access LAN from containers

假设我的 docker-compose 中有 2 个服务:

version: "2.4"

services:
  postgres:
    build: 
      context: .
      dockerfile: ./docker_config/dockerfiles/postgres.Dockerfile
    volumes:
      - ./docker_volumes/db/postgresql:/var/lib/postgresql    

  initializer:
    build:
      context: .
      dockerfile: ./docker_config/dockerfiles/initializer.Dockerfile
    depends_on: postgres

initializer 在为此 docker-compose 文件创建的网络中使用 "postgres" 主机名访问 postgres,但现在我希望 initializer 也进行交互LAN 中的某些主机,initializer 现在无法访问(所有请求都超时)。

我尝试在两个容器上和仅在 initializer 上使用 network-mode: bridge,LAN 访问有效,但在这些情况下 initializer 无法访问 postgres

如何从 initializer 容器访问 LAN 和 postgres 容器?

解决方案是向 initializer 服务添加另一个网络,而不是覆盖默认设置。为清楚起见,代码:

version: "2.4"

networks:
  lan_access:
    driver: bridge

services:
  postgres:
    build: 
      context: .
      dockerfile: ./docker_config/dockerfiles/postgres.Dockerfile
    volumes:
      - ./docker_volumes/db/postgresql:/var/lib/postgresql    

  initializer:
    build:
      context: .
      dockerfile: ./docker_config/dockerfiles/initializer.Dockerfile
    depends_on: postgres
    networks:
      - lan_access
      - default