E
E
Elena2018-06-07 22:46:06
PHP
Elena, 2018-06-07 22:46:06

In docker, a curl request from one container to another does not go?

There are 2 containers

version: '2'
services:
  php:
    build: ./
    restart: always
    volumes:
      - ~/.composer-docker/cache:/root/.composer/cache:delegated
      - ./:/app:delegated
    ports:
      - '9000:80'
    network_mode: test

  db_host:
    image: postgres:10.3-alpine
    restart: always
    ports:
      - "5432:5432"
    volumes:
      - ./docker-entrypoint:/docker-entrypoint-initdb.d
    environment:
      POSTGRES_USER: user
      POSTGRES_PASSWORD: password
    network_mode: test

docker file
FROM yiisoftware/yii2-php:7.1-apache

# Apache modules
RUN a2enmod rewrite

and second container
version: '2'
services:
  php:
    build: ../common/docker/
    restart: always
    volumes:
      - ~/.composer-docker/cache:/root/.composer/cache:delegated
      - ./:/app:delegated
    ports:
      - '9001:80'
    network_mode: test

  redis:
    image: redis:4.0.9-32bit
    ports:
      - "6379"
    volumes:
      - ../common/docker/redis-client-data:/data
    network_mode: test

From the browser, they all work great through localhost. Problems start when I try to make a request in the code from the second container to the first one via curl - http://container_name/api/ With this approach, I get a response from the server 400. I tried replacing the container name with an ip address. But on it there is a hangup and a timeout. By the way, directly in the browser the same reaction to ip. I can not understand what is the reason?
PS The container is running, the network is up and running.
PPS Works for others under the same conditions.
PPPS The most important thing for a snack is the working environment. I myself sit under Windows. A virtual machine is running on it and ubuntu (not a server) is deployed there.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
C
chupasaurus, 2018-06-08
@chupasaurus

There are 2 containers

docker-compose runs projects that are made up of services that are already made up of containers.
In order for containers from different networks to communicate without tambourines, they must be added to a separate bridge network (not docker0, which is by default, it has its own atmosphere):
  1. docker network create internal_bridge
  2. We write in both compose.yml:
    networks:
      bridge1:
        external:
          name: internal_bridge

  3. In the description of the services that should communicate with each other, you need to add individual names by which they will be knocked on:
    services:
      some-service:
        networks:
          bridge1:
            aliases:
              - some-service-project1

    After that, containers from different projects can knock to others using the prescribed aliases.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question