Search code examples
dockernginxdocker-composehealth-check

adding health check to docker container


I am trying to add a health check to my docker container so in my Dockerfile I added this line

HEALTHCHECK CMD curl --fail http://localhost:8080/health || exit 1

based loosely on this tutorial: https://howchoo.com/g/zwjhogrkywe/how-to-add-a-health-check-to-your-docker-container . In my docker-compose file I have added the health check line like this:

    healthcheck:
      test: ["CMD", "curl", "--silent", "--fail", "http://localhost:8080/health"]

but the container always reports unhealthy. So if I execute docker exec -it my-container /bin/bash and get inside the container and then execute the health request I get this:

    $ curl --fail http://localhost:8080/health
    curl: (22) The requested URL returned error: 411 Length Required

What am I missing? Nginx is already installed in the container so I would like to simply make that URL with /health work.


Solution

  • I gave up on using the HEALTHCHECK command in Dockerfile and instead I am changing the nginx.conf file by adding

        location /health {
            access_log off;
            return 200 "healthy\n";
        }
    

    docker-compose.yml remains the same. This works well enough for me.