I have a docker compose launching 2 containers. Problem is that it seems that my container with the api that I need to reach from the other container start only after and then I have a request issue.
version: "3.9"
services:
fastapi:
container_name: my-api
image: fastapi:latest
networks:
- my-network
ports:
- target: 8000
published: 8000
mode: host
authentification:
container_name: authentification
networks:
- my-network
image: authentification:latest
volumes:
- .:/home/log_folder
depends_on:
- "fastapi"
networks:
my-network:
Th containers can communicate together as If I launch manually the container with the script authentification after the api is started, it works.
Any clue ?
I finally added a time.sleep at he beginning of each of my container with script reaching the api to ensure they wait the api is launched. This is not a big issue as these scripts are just test script to check authentification etc ...
Thanks !
Dependency checks only container startup, not service. time.sleep is a crutch, actually. Just add healthcheck block to your fastapi service
fastapi:
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:8000"]
interval: 1s
timeout: 2s
retries: 10
and change dependency block to this:
authentification:
depends_on:
fastapi:
condition: service_healthy