简体   繁体   English

Docker组成的容器访问LAN

[英]Docker-compose access LAN from containers

Suppose I have 2 services in my docker-compose: 假设我在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 accesses postgres using "postgres" hostname in the network created for this docker-compose file, but now I want initializer to also interact with some host in LAN, which initializer can't access now (all requests just time out). initializer访问postgres使用"postgres"这个泊坞窗,撰写文件创建的网络中的主机名,但现在我想initializer也有一些局域网中的主机,它们相互作用initializer现在不能访问(所有请求都只是超时)。

I tried using network-mode: bridge on both containers and on initializer only, and LAN access worked, but initializer could not access postgres in these cases. 我尝试使用network-mode: bridge仅在容器和initializernetwork-mode: bridge ,并且可以进行LAN访问,但是在这些情况下initializer无法访问postgres

How to make both LAN and postgres container accessible from initializer container? 如何使LAN和postgres容器都可以从initializer容器访问?

The solution was to add another network to initializer service, not override the default. 解决方案是将另一个网络添加到initializer服务,而不要覆盖默认服务。 The code for clarity: 清楚的代码:

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

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM