Search code examples
pythondockerhealth-check

Docker health check always returning as unhealthy


Docker Healthcheck is failing, so its showing as unhealthy.

Here is the Dcokerfile

FROM python:3.8.5-alpine3.12

WORKDIR /app

EXPOSE 8080
ENV FLASK_APP=app.py

COPY . /app
RUN pip install -r requirements.txt

ENTRYPOINT [ "flask"]
HEALTHCHECK --interval=10s --timeout=10s --start-period=55s \
   CMD curl -f --retry 10 --max-time 15 --retry-delay 10 --retry-max-time 60 "http://localhost:8080/health" || exit 1   

CMD [ "run", "--host", "0.0.0.0", "--port",  "8080"]

Tried with below example, getting below error

$docker inspect --format '{{json .State.Health }}' flask-app

Template parsing error: template: :1:13: executing "" at <.State.Health>: map has no entry for key "State"

app.py

from flask import Flask, request
from flask_restx  import fields, Resource, Api, reqparse

app = Flask(__name__)
api = Api(app)
    
@api.route('/health', methods=['GET'])
class Health(Resource):
    def get(self):
        return { 'success': True, 'message': "healthy" }

if __name__ == '__main__':
    app.run(debug=True, host='0.0.0.0')

Health API is working fine

Notice 'Unhealthy' status in the docker ps


Solution

  • Are you sure you have curl installed in your container? It does not come with python:3.8.5-alpine3.12

    Add these lines to your Dockerfile

    RUN apk update && \
        apk add curl
        rm -rf /var/cache/apk/*