3

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.

3 Answers 3

6

An alternative approach, correlating a port being listened on with a healthy status.

netcat is installed by default in Alpine Linux

 nginx: image: nginx:1.17-alpine restart: unless-stopped healthcheck: test: ["CMD", "nc", "-vz", "-w1", "localhost", "80"] interval: 1s timeout: 1s retries: 30 

This will exit with status 0 if port 80 is open.

Sign up to request clarification or add additional context in comments.

Comments

4

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.

Comments

1

As a solution, you can use the following example - How to Add a Health Check to Your Docker Container

There's a minor correction to the Dockerfile they provide though as there's an issue to check the health status (curl had to be installed there as well but let me know if you need any help or have any question).

Please refer to this particular one as a solution -

FROM python:3.6-alpine COPY . /app WORKDIR /app RUN pip install -r requirements.txt RUN mkdir /data \ && apk add --no-cache \ openssl \ curl \ dumb-init \ postgresql-libs \ ca-certificates #CMD apt-get install curl HEALTHCHECK CMD curl -f http://localhost:5000/ || exit 1 CMD ["python", "app.py"] 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.