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"
应用程序.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')
最佳答案
您确定您的容器中安装了curl吗?它没有附带 python:3.8.5-alpine3.12
将这些行添加到您的 Dockerfile
RUN apk update && \
apk add curl
rm -rf /var/cache/apk/*
关于python - Docker 健康检查总是返回不健康状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67513998/