Search code examples
dockerdocker-composedocker-network

Docker-compose access LAN from containers


Suppose I have 2 services in my docker-compose:

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).

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.

How to make both LAN and postgres container accessible from initializer container?


Solution

  • The solution was to add another network to initializer service, not override the default. 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