Docker 健康检查总是返回不健康

时间:2021-05-13 04:46:25

标签: python docker health-check

Docker Healthcheck 失败,因此显示为不健康。

这是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"]

尝试下面的例子,得到以下错误

$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

2 个答案:

答案 0 :(得分:1)

您确定您的容器中安装了 curl 吗?它没有 python:3.8.5-alpine3.12

将这些行添加到您的 Dockerfile

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

答案 1 :(得分:0)

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

去掉 (json) 上的 {{json .State.Health }} 后面多余的空格