0

For past 3 days I have been trying to connect two docker containers generated by two separate docker-compose files.

I tried a lot of approaches none seem to be working. Currently after adding network to compose files, service with added network doesn't start. My goal is to access endpoint from another container.

Endpoint-API compose file:

    version: "3.1"

    networks:
      test:

    services:
        mariadb:
          image: mariadb:10.1
          container_name: list-rest-api-mariadb
          working_dir: /application
          volumes:
            - .:/application
          environment:
            - MYSQL_ROOT_PASSWORD=root
            - MYSQL_DATABASE=list-api
            - MYSQL_USER=list-api
            - MYSQL_PASSWORD=root
          ports:
            - "8003:3306"

        webserver:
          image: nginx:stable
          container_name: list-rest-api-webserver
          working_dir: /application
          volumes:
              - .:/application
              - ./docker/nginx/nginx.conf:/etc/nginx/conf.d/default.conf
          ports:
           - "8005:80"
          networks:
           - test

        php-fpm:
          build: docker/php-fpm
          container_name: list-rest-api-php-fpm
          working_dir: /application
          volumes:
            - .:/application
            - ./docker/php-fpm/php-ini-overrides.ini:/etc/php/7.2/fpm/conf.d/99-overrides.ini

Consumer compose file:

version: "3.1"

networks:
  test:

services:
    consumer-mariadb:
      image: mariadb:10.1
      container_name: consumer-service-mariadb
      working_dir: /consumer
      volumes:
        - .:/consumer
      environment:
        - MYSQL_ROOT_PASSWORD=root
        - MYSQL_DATABASE=consumer
        - MYSQL_USER=consumer
        - MYSQL_PASSWORD=root
      ports:
        - "8006:3306"

    consumer-webserver:
      image: nginx:stable
      container_name: consumer-service-webserver
      working_dir: /consumer
      volumes:
          - .:/consumer
          - ./docker/nginx/nginx.conf:/etc/nginx/conf.d/default.conf
      ports:
       - "8011:80"
      networks:
       - test

    consumer-php-fpm:
      build: docker/php-fpm
      container_name: consumer-service-php-fpm
      working_dir: /consumer
      volumes:
        - .:/consumer
        - ./docker/php-fpm/php-ini-overrides.ini:/etc/php/7.2/fpm/conf.d/99-overrides.ini

Could someone tell me best way of accessing API container from within consumer? I'm loosing my mind on this one.

Maarduk
  • 246
  • 4
  • 17
  • How are you trying to access the API? Are you using it in a virtual host file? What's the error you get? Did you have a look at this https://stackoverflow.com/questions/38088279/communication-between-multiple-docker-compose-projects? – Sorix Aug 18 '19 at 11:07
  • Hmm, I'm totally green in docker, I'm just entering consumer-webserver container and trying to curl into rest container, no idea if that answers your question... I think i tried the approach from the post you linked, Ill try again tho – Maarduk Aug 18 '19 at 11:11
  • I tried this way, when I do that container doesn't start, after building it again it looks like it should be up but docker ps doesn't show this container. – Maarduk Aug 18 '19 at 11:20
  • Once you correctly setup your docker containers, if it still doesn't work, go into the API container and get the IP address. Then go into your consumer container and try pinging it. – Sorix Aug 18 '19 at 11:20
  • Yeah that approach works, but I'm not sure if it's correct because the IP would be changing upon resetting docker..? – Maarduk Aug 18 '19 at 11:26
  • Also, you need to configure correctly Nginx both on the consumer and the API side, to have the virtual hosts pointing to the correct addresses and ports. Is that something you have already done? – Sorix Aug 18 '19 at 11:26
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/198096/discussion-between-sorix-and-maarduk). – Sorix Aug 18 '19 at 11:26

1 Answers1

2

Your two

    networks:
      test:

don't refer to the same network, but they are independent and don't talk to each other.

What you could do is having an external and pre-existing network that both compose file can talk and share. You can do that with docker network create, and then refer to that in your compose files with

networks:
  default:
    external:
      name: some-external-network-here
Federkun
  • 36,084
  • 8
  • 78
  • 90
  • Yay! Finally it works! Thaaaank you soooo much! You are my savior :) Btw. Is it possible to make those two separate containers communicate somehow, throught I dont know, localhost for example? Some approach that wouldn't need establishing network? – Maarduk Aug 18 '19 at 11:28
  • I think so, yeah. You can add `network_mode: host` to all your services, but: you can't map ports ( and you may have some "port already used" issues ) – Federkun Aug 18 '19 at 11:41