【问题标题】:Docker-compose access LAN from containersDocker-compose 从容器访问局域网
【发布时间】:2019-03-19 20:36:22
【问题描述】:

假设我的 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。

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

【问题讨论】:

    标签: docker docker-compose docker-networking


    【解决方案1】:

    解决方案是向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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-10-18
      • 2021-03-04
      • 2021-12-08
      • 2021-07-19
      • 1970-01-01
      • 2017-10-12
      • 1970-01-01
      相关资源
      最近更新 更多