Docker Community Forums

Share and learn in the Docker community.

Healthcheck in compose file blocks starting container

I’m using the following compose file:
version: '2.1’
services:

  sqlcl:
    image: myClientImage
    networks:
        - frontend
    depends_on:
        db:
            condition: service_healthy

db:
    image: wnameless/oracle-xe-11g
    environment: 
        - ORACLE_SID=''
    networks:
        - frontend
    healthcheck:
        test: ["CMD", "if [ ! -z $ORACLE_SID ]", "then echo 'done!' && exit 0", "else echo 'not yet' && exit 1", "fi"]
        interval: 30s
        timeout: 1s
        retries: 1

networks:
 frontend:

The db container uses a script as ENTRYPOINT that actually starts the db server and then sets the environment variable ORACLE_SID. What I want to achieve is that my client waits for the server to be readily started. According to the docs, that’s what healtchcheck should do.
What actually happens is that starting the db container is blocked and the client fails with db is unhealthy. What am I doing wrong here?
When I remove the healthcheck related config both containers start, the client however before the server is ready.

you could add a restart policy to your services.

restart: unless-stopped

This will give your sqlcl a second chance if your db container is not ready at the first try.

Details here: (this is for compose version 3, but restart policies are also in 2.0 and your 2.1 available)

I don’t think that this is just a timing issue. As I stated, to me it appears as if the container itself waits to become healthy before executing the ENTRYPOINT command. Which would be weird.
Eg I docker run the container from the image and see that it is up with 1 min or so. However, when I use compose up with the health check, it reports as unhealthy after 5 min using interval: 1m, timeout 1s, retries 5

did you try your healthcheck command? it currently doesn’t really help you anything. it checks an immutable environment which you just provided in a loop.
ORACLE_SID also never gets a different value, so better use another healthcheck.

So to mitigate the probably broken health check a restart policy could help. But you are right, it’s better to work on the health check.

Isn’t there a way to connect to the database and execute a very simple query? This would be an ideal health check.

@cmurczek Did you find a solution to this? I believe I’m running in to something similar.