0

Can a services running in two different docker stack communicate ?

This is what I have tried and not able to achieve it.

  1. Created a stack (stack1) running nginx and php-fpm its running great.
  2. Created another stack (stack2) running mysql database.
  3. Now I want to make the stack1 service able to communicate with stack2 such that it can access the database service.

I though this might help and created a external network and trying to add the service in stack1 and stack2 to it such that they can communicate with each others too

Mystack1 docker-compose file

version: "3.4"
networks:
    apps-net:
    db-net:
        external:
            name: db-net
services:
    web:
        image: nginx:latest
        ports:
            - "9080:80"
        volumes:
            - ./code:/code
            - ./site.conf:/etc/nginx/conf.d/default.conf

        depends_on:
            - php
        networks:
            - apps-net
            - db-net

        deploy:
            mode: replicated
            replicas: 1
    php:
        image: php:7-fpm
        volumes:
            - ./code:/code
        networks:
            - apps-net
            - db-net
        deploy:
            mode: replicated
            replicas: 1

MY stack2 docker-compose file

version: '3.3'
networks:
    db-net:
        external:
            name: db-net

services:
    db:
        image: mysql:5.7
        volumes:
            - db_data:/var/lib/mysql
        restart: always
        environment:
            MYSQL_ROOT_PASSWORD: password
            MYSQL_DATABASE: wordpress
            MYSQL_USER: root
            MYSQL_PASSWORD: password
        networks:
            - db-net

volumes:
    db_data:

I created a swarm scope network with docker network create db-net command

OUTPUT:

The nginx and php is working fine but I added the database connection codes in the index.php which resulted in error message.Is the error because they are not connected? I have installed php-mysql extensions too but it has the error. How can I make sure the services are communicating successfully.

nginx and php working
Fatal error: Uncaught Error: Call to undefined function mysqli_connect() in /code/index.php:11 Stack trace: #0 {main} thrown in /code/index.php on line 11
Tara Prasad Gurung
  • 3,422
  • 6
  • 38
  • 76

1 Answers1

0

Instead of networks (which I often see asked here on stack as not working like expected) try using external_links which is well explained here.

Try removing all custom network configurations and simply modify your application compose file like this:

version: "3.4"
# removed all custom networks configuration

services:
    web:
        image: nginx:latest
        external_links:
           - mysql_1:mysql
        [..]
    php:
        image: php:7-fpm
        external_links:
           - mysql_1:mysql
        [..]

where mysql_1 is a actual container name create by your latter compose file, and mysql is a alias by which your service will be available inside php and web containers

Links are a legacy option in v3 and Docker suggests using networks instead. I'll post a edit about swarm deployment as approach would be totally different because Docker ignores links when deploying swarm.

trust512
  • 2,188
  • 1
  • 18
  • 18