KorbenDallas
KorbenDallas

Reputation: 1044

Communication between several php docker containers

I have several php servers, let's name them api1, api2. I've set up docker-compose file which successfully run them in link with nginx, so they are accessible from host machine and working fine.

Here is example of my docker-compose.yml:

version: "3.5"
services:
nginx:
    user: "root:root"
    image: nginx:stable
    container_name: nginx
    ports:
        - "80:80"
        - "443:443"
    volumes:
        - ./cert:/etc/nginx/ssl/
        - ./vhosts/:/etc/nginx/conf.d/:cached
        - ./logs/nginx:/var/log/nginx:cached
        - ${ACCOUNT_PATH}:/var/www/api1:cached
        - ${ACCOUNT_API_PATH}:/var/www/api2:cached
api1:
    image: php-fpm-image
    build:
        context: "."
        dockerfile: "./images/api1/Dockerfile"
    user: "root:root"
    container_name: account
    working_dir:  /var/www/api1/
    volumes:
        - api1path/:/var/www/api1
api2:
    image: php-fpm-image
    build:
        context: "."
        dockerfile: "./images/api2/Dockerfile"
    user: "root:root"
    container_name: api2
    working_dir:  /var/www/api2/
    volumes:
        - api2:/var/www/api2:cached
networks:
    default:
       name: domain.com
       driver: bridge

In connection with this docker-compose file for functional tests:

     version: '3.5'
     services:
        apitests:
           image: apitests:latest
           container_name: api_tests
           volumes:
             - ./config/:/opt/project/config/
             - ./test_reports/screenshots:/root/.selene/screenshots/
     networks:
       default:
       driver: bridge
       external:
          name: domain.com

There are next domains which are accessible from host machine:

api1.domain.com 127.0.0.1

api2.domain.com 127.0.0.1

My problem is how to connect them directly inside docker, because I need to do requests from api1 to api2, apitests to api1, api2.

When I do request, their domain resolving directly to their containers, so I receive next error

can't connect to remote host (172.21.0.8): Connection refused

from any container.

As you understand, I need that their domains resolve to NGINX container, so it can work correctly and php-fpm can return result via nginx back to me.

How do can I achieve this?

Upvotes: 2

Views: 1202

Answers (2)

Ivan Ovcharenko
Ivan Ovcharenko

Reputation: 372

It seems that your problem is that you've named php container as api1.domain.com when your requirements are to assign this domain name for nginx container.

You could assign api1/api2 aliases to nginx inside container networks.

services:
   nginx:
      networks:
        default:
          aliases:
            - api1
            - api2

Upvotes: 2

grapes
grapes

Reputation: 8636

If I got you right, you have several docker-compose.yml files and need services from them to interact with each other. The logic suggestion is to have a globally-defined network, lets say

docker network create testapis

and have all services linked to it:

docker-compose.yml

...
networks:
  default:
    external: 
      name: testapis

In this case all services from all docker-compose files will see each other under their hostnames (api1, api2, etc) and no port exposing would be needed (unless you want to use services from outside this net)

Upvotes: 0

Related Questions