I am trying to make sure that my app container does not run migrations / start until the db container is started and READY TO accept connections.
So I decided to use
Hi for a simple healthcheck using docker-compose v2.1, I used:
/usr/bin/mysql --user=root --password=rootpasswd --execute \"SHOW DATABASES;\"
Basically it runs a simple mysql command SHOW DATABASES; using as an example the user root with the password rootpasswd in the database.
If the command succeed the db is up and ready so the healthcheck path. You can use interval so it tests at interval.
Removing the other field for visibility, here is what it would look like in your docker-compose.yaml.
version: '2.1'
services:
db:
... # Other db configuration (image, port, volumes, ...)
healthcheck:
test: "/usr/bin/mysql --user=root --password=rootpasswd --execute \"SHOW DATABASES;\""
interval: 2s
timeout: 20s
retries: 10
app:
... # Other app configuration
depends_on:
db:
condition: service_healthy