How to exchange localhost between two different Docker containers?

I have two different Docker containers, and each has a different image. Each container application uses non-conflicting ports. See 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 successfully compose each image from the host machine (Mac OS), for example. curl -k https://localhost:6473/service_a/api/versionworks. What I would like to do is be able to refer to the container postgresfrom the container service_a throughlocalhost , as if the two containers were the same and they have the same one localhost, I know that it is possible if I use the host name postgres.devfrom the container service_a, but I would like to use localhost. Is it possible? Please note that I am not very good at networking or Docker.

Mac version: 10.12.4

: 17.03.0-ce, build 60ccb22

, . : https://forums.docker.com/t/localhost-and-docker-compose-networking-issue/23100/2

0
2

: localhost. , DNS, . , .


: , , , . , :

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/

, , . hostname, .

, , , , , localhost. , localhost. VirtualBox docker-toolbox, IP-.


: . , DNS. , , .

, , docker-compose.yml, docker-compose , - . postgres:

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

:

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
+1

Source: https://habr.com/ru/post/1683183/


All Articles