Since you are already using docker-compose
to orchestrate your services a better way would be to use condition: service_healthy
of the depends_on
long syntax. So instead of manually waiting in one container for the other to become available docker-compose
will start the former only after the latter became healthy
, i.e. available.
If the depended-on container does not have a specified HEALTHCHECK
in its image already you can manually define it in the docker-compose.yml
with the healthcheck
attribute.
Example with a mariadb
database using the included healthcheck.sh
script:
services:
app:
image: myapp/image
depends_on:
db:
condition: service_healthy
db:
image: mariadb
environment:
- MARIADB_ROOT_PASSWORD=password
healthcheck:
test: "healthcheck.sh --connect"
With this docker-compose up
will first start the db
service and wait until the db
service becomes healthy
, i.e. is ready to accept connections, and only then will start the app
service which can immediately connect to the db
.