'Docker. Communication between Laravel containers

I have a project that I am trying to develop with a microservices approach. I prepared the APIs on two separate microservices and completed their tests. When I send a request from container A to container B, I get the error cURL error 7: Could not connect to localhost port 8100: Connection refused. After my research, I couldn't find a solution. Request Architecture: I'm trying to make a request from 127.0.0.1:8000 to an api address running at 127.0.0.1:8100.

What should I do to fix this problem? My question may be wrong, so I apologize in advance.

thanks

My Compose File First Container

# For more information: https://laravel.com/docs/sail
version: '3'
services:
    laravel.test:
      build:
          context: ./docker/8.0
          dockerfile: Dockerfile
          args:
              WWWGROUP: '${WWWGROUP}'
      image: sail-8.0/app
      container_name: MP-Main-Service-Laravel
      extra_hosts:
          - 'host.docker.internal:host-gateway'
      ports:
          - 8000:8000
      environment:
          WWWUSER: '${WWWUSER}'
          LARAVEL_SAIL: 2
          XDEBUG_MODE: '${SAIL_XDEBUG_MODE:-off}'
          XDEBUG_CONFIG: '${SAIL_XDEBUG_CONFIG:-client_host=host.docker.internal}'
      volumes:
          - '.:/var/www/html'
      networks:
          - sail
      depends_on:
          - mysql
          - redis
    mysql:
        image: 'mysql:8.0'
        container_name: MP-Main-Service-MySql
        ports:
            - '${FORWARD_DB_PORT:-3306}:3306'
        environment:
            MYSQL_ROOT_USER: '${DB_USERNAME}'
            MYSQL_ROOT_PASSWORD: '${DB_PASSWORD}'
            MYSQL_DATABASE: '${DB_DATABASE}'
            MYSQL_USER: '${DB_USERNAME}'
            MYSQL_PASSWORD: '${DB_PASSWORD}'
            MYSQL_ALLOW_EMPTY_PASSWORD: 'yes'
        volumes:
            - "/var/lib/mysql"
        networks:
            - sail
    phpmyadmin:
        image: 'phpmyadmin:latest'
        container_name: MP-Main-Service-PhpMyAdmin
        ports:
            - 8081:80
        environment:
            PMA_HOST: mysql
            MYSQL_ROOT_USER: '${DB_USERNAME}'
            MYSQL_ROOT_PASSWORD: '${DB_PASSWORD}'
            MYSQL_DATABASE: '${DB_DATABASE}'
        depends_on:
            - mysql
        networks:
            - sail
    redis:
        image: 'redis:alpine'
        container_name: MP-Main-Service-Redis
        ports:
            - '${FORWARD_REDIS_PORT:-6379}:6379'
        volumes:
            - 'sailredis:/data'
        networks:
            - sail
        healthcheck:
          test: ["CMD", "redis-cli", "ping"]
          retries: 3
          timeout: 5s
networks:
    sail:
        driver: bridge
volumes:
    sailmysql:
        driver: local
    sailredis:
        driver: local
 

My Compose File Second Container

# For more information: https://laravel.com/docs/sail
version: '3'
services:
    laravel.test:
        build:
            context: ./docker/8.0
            dockerfile: Dockerfile
            args:
                WWWGROUP: '${WWWGROUP}'
        image: sail-8.0/app
        container_name: MP-Communication-Service-Laravel
        extra_hosts:
            - 'host.docker.internal:host-gateway'
        ports:
            - 8100:8000
        environment:
            WWWUSER: '${WWWUSER}'
            LARAVEL_SAIL: 1
            XDEBUG_MODE: '${SAIL_XDEBUG_MODE:-off}'
            XDEBUG_CONFIG: '${SAIL_XDEBUG_CONFIG:-client_host=host.docker.internal}'
        volumes:
            - '.:/var/www/html'
        networks:
            - sail
        depends_on:
            - mysql
            - redis
    mysql:
        image: 'mysql:8.0'
        container_name: MP-Communication-Service-MySql
        ports:
            - '${FORWARD_DB_PORT:-33061}:33061'
        environment:
            MYSQL_ROOT_USER: '${DB_USERNAME}'
            MYSQL_ROOT_PASSWORD: '${DB_PASSWORD}'
            MYSQL_DATABASE: '${DB_DATABASE}'
            MYSQL_USER: '${DB_USERNAME}'
            MYSQL_PASSWORD: '${DB_PASSWORD}'
            MYSQL_ALLOW_EMPTY_PASSWORD: 'yes'
        volumes:
            - "/var/lib/mysql"
        networks:
            - sail
    phpmyadmin:
        image: 'phpmyadmin:latest'
        container_name: MP-Communication-Service-PhpMyAdmin
        ports:
            - 8082:80
        environment:
            PMA_HOST: mysql
            MYSQL_ROOT_USER: '${DB_USERNAME}'
            MYSQL_ROOT_PASSWORD: '${DB_PASSWORD}'
            MYSQL_DATABASE: '${DB_DATABASE}'
        depends_on:
            - mysql
        networks:
            - sail
    redis:
        image: 'redis:alpine'
        container_name: MP-Communication-Service-Redis
        ports:
            - '${FORWARD_REDIS_PORT:-63791}:6379'
        volumes:
            - 'sailredis:/data'
        networks:
            - sail
        healthcheck:
            test: ["CMD", "redis-cli", "ping"]
            retries: 3
            timeout: 5s
networks:
    sail:
        driver: bridge
volumes:
    sailmysql:
        driver: local
    sailredis:
        driver: local



Solution 1:[1]

Notice: this solution is raw and for development time:

First get container gateway ip:

? docker inspect {container id} | grep -i Gateway                                                                                                                                                                                      

Second Suppose it gives you 172.25.0.1

Your url pattern will look like this:

http://{containerIp}:{port}/...

or:

http://172.25.0.1:8585/...

The port is the same as the one you put in your docker-compose.yaml file.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 MohNj