skip to Main Content

I have two dockerized laravel app, both are going to be used as an API.

One is the Main API, and the other is the Payment API

Docker compose of Main API:

version: '3.8'
services:
    api:
        image: 'myapp/api:1.0'
        container_name: 'myapp-api'
        restart: 'on-failure'
        user: '1000:1000'
        build:
            context: .
            dockerfile: '.docker/Dockerfile'
            args:
                UID: '1000'
                GID: '1000'
        ports:
            - '${APP_PORT:-80}:80'
        volumes:
            - '.:/var/www/html'
        networks:
            - myapp
networks:
    myapp:
        driver: bridge

Docker compose of Payment API:

version: '3.8'
services:
    payment-api:
        image: 'myapp/payment-api:1.0'
        container_name: 'myapp-payment-api'
        restart: 'on-failure'
        user: '1000:1000'
        build:
            context: .
            dockerfile: '.docker/Dockerfile'
            args:
                UID: '1000'
                GID: '1000'
        ports:
            - '${APP_PORT:-801}:80'
        volumes:
            - '.:/var/www/html'
        networks:
            - myapp
networks:
    myapp:
        driver: bridge

From the Main API, I tried calling the health check of Payment API:

use IlluminateSupportFacadesHttp;
Http::get('http://payment-api/api/health_check');

but I am getting cURL error:

cURL error 6: Could not resolve host: payment-api (see https://curl.haxx.se/libcurl/c/libcurl-errors.html) for http://payment-api/api/health_check

Note that health_check doesn’t have any middleware attached to it. just plain and simple route to test if the route is reachable.

doing http://localhost:801/api/health_check in the browser works. but wont work when called inside the laravel app.

I am using the container’s name as the host here since http://localhost doesn’t work either.

2

Answers


  1. Chosen as BEST ANSWER

    Okay, after hours of troubleshooting, I found out that there's a command docker network ls, and my network isn't listed. So I searched, and noticed that the way I defined my network is the legacy-way, so maybe because of that that it wasn't created. Another is, for my Payment API container, I didn't specify the network as external, so even if the network was created, it still probably wont work because docker compose would've assumed that the network is not external and wont be in the same network as the Main API.


  2. Make sure your two container network is same, because docker compose will prefix your network name by folder name.

    How to check:

    1. Check both container settings.

    use docker inspect <containerName or containerId>, replace "<...>" to your container name or container id, and check NetworkSettings > Networks, two container Networks should be same.

    // Example
    "NetworkSettings": {
        ...
        "Networks": {
            "my-app": { ... } // <- check Network name
        }
    }
    
    1. Check from network.

    use docker network inspect <networkName>, and check your container are using same network.

    // Example
    ...
    "Containers": {
        ...
        "2248c433dae5d0a1b08bdd11dad86184785b89e269b42a76806b11cf6fbaccfa": {
            "Name": "myapp-api", // <- should see your first container name
            ...
        },
        "3b64e3d359e13bdae60bbfe283a76516ca51678da69c0c81b6e83be315aea8f2": {
            "Name": "myapp-payment-api", // <- should see your second container name
            ...
        },
        ...
    },
    

    If this is what you are facing and your don’t want docker compose prefix, can just simple add "name" in your yaml.

    networks:
        myapp:
            driver: bridge
            name: myapp // like this
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search