How to share localhost between two different Docker containers?

I have two different Docker containers and each has a different image. Each app in the containers uses non-conflicting ports. See the docker-compose.yml:

version: "2"

services:

  service_a:
    container_name: service_a.dev
    image: service_a.dev
    ports:
      - "6473:6473"
      - "6474:6474"
      - "1812:1812"
    depends_on:
      - postgres
    volumes:
      - ../configs/service_a/var/conf:/opt/services/service_a/var/conf

  postgres:
    container_name: postgres.dev
    hostname: postgres.dev
    image: postgres:9.6
    ports:
      - "5432:5432"
    volumes:
      - ../configs/postgres/scripts:/docker-entrypoint-initdb.d/

I can cURL each image successfully from the host machine (Mac OS), e.g. curl -k https://localhost:6473/service_a/api/version works. What I’d like to do is to be able to refer to postgres container from the service_a container via localhost as if these two containers were one and they share the same localhost. I know that it’s possible if I use the hostname postgres.dev from inside the service_a container, but I’d like to be able to use localhost. Is this possible? Please note that I am not very well versed in networking or Docker.

  • connect to rethinkdb running in docker kitematic
  • read nested variable from .env for laravel 5.1 (and docker)
  • “ECONNREFUSED” while connecting to docker mysql container in NodeJS application
  • How to use vagrant docker-run with container environment variable expansion
  • How to replace volumes_from in docker-composer v3
  • Docker Container Networking one container on one network
  • Mac version: 10.12.4

    Docker version: Docker version 17.03.0-ce, build 60ccb22

    I have done quite some prior research, but couldn’t find a solution.
    Relevant: https://forums.docker.com/t/localhost-and-docker-compose-networking-issue/23100/2

  • Socketplane and Docker : How to connect 2 (or more) physical hosts with each others
  • Volume changes not persistent after “docker-compose run” command (Django's collectstatic)
  • Creating new container with volume from an older doesn't work as expected
  • neo4j-mazerunner, How to Increase memory size in docker-compose.yml
  • How to run etcd cluster in docker using ansible?
  • docker container fails to start with docker-compose exit status 2, code 128
  • One Solution collect form web for “How to share localhost between two different Docker containers?”

    The right way: don’t use localhost. Instead use docker’s built in DNS networking and reference the containers by their service name. You shouldn’t even be setting the container name since that breaks scaling.


    The bad way: if you don’t want to use the docker networking feature, then you can switch to host networking, but that turns off a very key feature and other docker capabilities like the option to connect containers together in their own isolated networks will no longer work. With that disclaimer, the result would look like:

    version: "2"
    
    services:
    
      service_a:
        container_name: service_a.dev
        image: service_a.dev
        network_mode: "host"
        depends_on:
          - postgres
        volumes:
          - ../configs/service_a/var/conf:/opt/services/service_a/var/conf
    
      postgres:
        container_name: postgres.dev
        image: postgres:9.6
        network_mode: "host"
        volumes:
          - ../configs/postgres/scripts:/docker-entrypoint-initdb.d/
    

    Note that I removed port publishing from the container to the host, since you’re no longer in a container network. And I removed the hostname setting since you shouldn’t change the hostname of the host itself from a docker container.

    The linked forum posts you reference show how when this is a VM, the host cannot communicate with the containers as localhost. This is an expected limitation, but the containers themselves will be able to talk to each other as localhost. If you use a VirtualBox based install with docker-toolbox, you should be able to talk to the containers by the virtualbox IP.


    The really wrong way: abuse the container network mode. The mode is available for debugging container networking issues and specialized use cases and really shouldn’t be used to avoid reconfiguring an application to use DNS. And when you stop the database, you’ll break your other container since it will lose its network namespace.

    For this, you’ll likely need to run two separate docker-compose.yml files because docker-compose will check for the existence of the network before taking any action. Start with the postgres container:

    version: "2"
    services:
      postgres:
        container_name: postgres.dev
        image: postgres:9.6
        ports:
          - "5432:5432"
        volumes:
          - ../configs/postgres/scripts:/docker-entrypoint-initdb.d/
    

    Then you can make a second service in that same network namespace:

    version: "2"
    services:
      service_a:
        container_name: service_a.dev
        image: service_a.dev
        network_mode: "container:postgres.dev"
        ports:
          - "6473:6473"
          - "6474:6474"
          - "1812:1812"
        volumes:
          - ../configs/service_a/var/conf:/opt/services/service_a/var/conf
    
    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.