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
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.Make sure your two container network is same, because docker compose will prefix your network name by folder name.
How to check:
use
docker inspect <containerName or containerId>
, replace"<...>"
to your container name or container id, and check NetworkSettings > Networks, two container Networks should be same.use
docker network inspect <networkName>
, and check your container are using same network.If this is what you are facing and your don’t want docker compose prefix, can just simple add "name" in your yaml.