S
S
Sergey Ilichev2021-06-16 13:28:00
Docker
Sergey Ilichev, 2021-06-16 13:28:00

How to communicate between docker containers?

Hello colleagues.

There are different services, for example, let's take two service-a (php, nginx) and service-b

service-a

version: '3.5'

networks:
  main:
    driver: bridge
    name: main
    ipam:
      driver: default
      config:
        - subnet: 172.26.13.0/24

services:
  php-service-a:
    build:
      context: .
      dockerfile: docker/php-extended/Dockerfile
      args:
        ssh_prv_key: ""
    volumes:
      - ./:/app
      - "phpsocket:/var/run"
      - ~/.ssh:/root/.ssh
    networks:
      - main

  nginx-service-a:
    build:
      context: .
      dockerfile: docker/nginx/Dockerfile
    ports:
      - "${NGINX_PORT}:80"
    volumes:
      - ./:/app
      - "phpsocket:/var/run"
    depends_on:
      - php-service-a
    networks:
      - main

volumes:
  phpsocket:


service-b

version: '3.5'

networks:
  main:
    external:
      name: main

services:
  service-b:
    build:
      context: .
      dockerfile: docker/golang/local.Dockerfile
    working_dir: /go/app
    volumes:
      - ./:/go/app
    ports:
      - "${GO_PORT}:8080"
    environment:
      - DB_USERNAME
      - DB_PASSWORD
      - DB_HOST
      - DB_EXTERNAL_PORT
      - DB_DB
    networks:
      - main
    tty: true


Here GO_PORT = 8027.

In the first service we create the main network, in the second service we use it as an external one.

We start all containers. We go into the container of service A. There we execute a request with a curl to the address service-b: 8027 - it says that the host is not correct, also with a request to localhost. Only request to host.docker.internal:8027 works. Please explain why? It seems that the request should work by the name of the service inside the container.

Outside, everything is clear, it only works on localhost: 8027, since the service-b host is not registered by default. But why the service is not available inside the container either by host name or by localhost, this is strange. 127.0.0.1:8027 also tried inside the container.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vasily Bannikov, 2021-06-16
@vabka


Only request to host.docker.internal:8027 works. Please explain why? It seems that the request should work by the name of the service inside the container.

Names only work within the same compose, just like networks.
Push both services into one compose, since they depend on each other.
Containers are defined by the dockerfile, which is why localhost does not work, because these are all different containers

M
montray, 2021-06-17
@quiex

There is no need to stuff anything into one docker-compose.yml, everything should work like that.
Docker-compose is just a wrapper around the standard docker api, so it's the same as running containers through docker run.
Why, specifically, it does not work for you, I can’t say for sure.
First look at docker network ls to see if compose creates a network with prefixes/suffixes.
Try to create a network from the outside, and in both files write external.
Well, and also, perhaps it depends on the OS on which you are doing all this. (On a poppy, for example, everything works)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question