繁体   English   中英

向 docker 容器添加健康检查

[英]adding health check to docker container

我正在尝试向我的 docker 容器添加健康检查,所以在我的 Dockerfile 中我添加了这一行

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

大致基于本教程: https://howchoo.com/g/zwjhogrkywe/how-to-add-a-health-check-to-your-docker-container 在我的 docker-compose 文件中,我添加了这样的健康检查行:

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

但是容器总是报告不健康。 因此,如果我执行docker exec -it my-container /bin/bash并进入容器,然后执行健康请求,我会得到:

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

我错过了什么? Nginx 已经安装在容器中,所以我只想让 URL 与/health一起工作。

我放弃了在Dockerfile中使用HEALTHCHECK命令,而是通过添加更改nginx.conf文件

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

docker-compose.yml保持不变。 这对我来说足够好。

另一种方法,将正在侦听的端口与健康状态相关联。

netcat默认安装在 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

如果端口80打开,这将以状态 0 退出。

作为解决方案,您可以使用以下示例 - 如何将健康检查添加到您的 Docker 容器

他们提供的 Dockerfile 有一个小的更正,尽管存在检查健康状态的问题(也必须在那里安装 curl,但如果您需要任何帮助或有任何问题,请告诉我)。

请参考这个特定的解决方案 -

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM