I have two separate docker containers, each running an api (auth_api and user_api).
I am trying to make a get request from the user_api
service to the auth_api
service but it fails with this error: curl: (6) Could not resolve host: auth_api
These are my docker-compose files:
/auth
version: '3'
services:
auth_api:
build:
context: .
command: uvicorn main:app --host 0.0.0.0
ports:
- "8000:8000"
networks:
default:
name: shopnet
external: false
/user
version: '3'
services:
user_api:
build:
context: .
command: uvicorn main:app --host 0.0.0.0 --port 8001
ports:
- "8001:8001"
networks:
shopnet:
external: true
The shopnet
network is created because it shows up in the list: docker network ls
I am executing the GET request with curl directly from the user_api
container:
curl http://auth_api:8000/auth
3
Answers
Seems like adding
to the user_api service fixes the problem
Docker doesn’t use
_
in the hostname anymore.Unless you didn’t update for ages, replace
_
by-
.https://github.com/docker/compose/issues/229
The
user_api
service doesn’t have anetworks:
block, so it connects to thedefault
network that Compose automatically provides. You declare a separateshopnet
network, but don’t attach your network to it. (Your answer addresses this by explicitly naming the other network.)You can configure the
default
network to make the "user"default
network be the same as the "auth"default
network:You also may be able to put both services in the same
docker-compose.yml
file, depending on your specific setup, and then you wouldn’t need any manualnetworks:
setup at all.