docker-compose healthcheck does not work in a way it is expected for making container a run first and then container B
The two examples are based on the condition
form of depends_on
which is no longer supported in compose version 3. So, unless your docker-compose version is <3 the healthcheck
will not help you much. The healthcheck
sets the status of the container (starting, healthy or unhealthy) but docker-compose
does not wait until backend
container is healthy before starting the app-test
. There is a detailed explanation about how depends_on
works in Control startup and shutdown order in Compose
As a side note, the healthcheck in your compose file sets the status of the app-test
container and not backend
.
Therefore to control when the api-test
can start, you have to wrap the service command of the container. For your particular case the following will do the job:
bash -c 'while [[ "$(curl --connect-timeout 2 -s -o /dev/null -w ''%{http_code}'' https://backend:3015/readiness)" != "200" ]]; do echo ..; sleep 5; done; echo backend is up; <service_command>'
It tries to connect to backend
every 5 seconds (the connection timeout is 2s). When the received HTTP status code is 200 OK the loop ends and it executes the <service_command>
The relevant docker-compose part:
api-test:
restart: always
command: bash -c 'while [[ "$$(curl --connect-timeout 2 -s -o /dev/null -w ''%{http_code}'' uds-mock-server:4000/readiness)" != "200" ]]; do echo ..; sleep 5; done; echo backend is up;npm start'
depends_on:
- backend
...
Hope this helps.
it's not localhost
connection string.
It should be service name of backend container :
test: ["CMD", "curl", "-f", "http://backend:3015/readiness"]