gpt4 book ai didi

nginx 容器的 Docker 健康检查

转载 作者:行者123 更新时间:2023-12-01 09:14:15 26 4
gpt4 key购买 nike

我有一个项目使用官方nginx来自 Docker Hub 的 docker 容器,通过 Docker Compose 启动。我在 Docker Compose 中为我的每个容器配置了健康检查,最近为这个 nginx 配置了健康检查。容器行为异常;与 docker-compose up -d 一起启动,我的所有容器都启动并开始运行健康检查,但是 nginx容器看起来从不运行健康检查。如果我 docker exec,我可以手动运行脚本就好了进入容器,如果我重新启动容器,则健康检查正常运行。

来自 docker ps 的示例输出:

CONTAINER ID        IMAGE                     COMMAND                  CREATED             STATUS                            PORTS                                                                       NAMES
458a55ae8971 my_custom_image "/tini -- /usr/local…" 7 minutes ago Up 7 minutes (healthy) project_worker_1
5024781b1a73 redis:3.2 "docker-entrypoint.s…" 7 minutes ago Up 7 minutes (healthy) 127.0.0.1:6379->6379/tcp project_redis_1
bd405dde8ce7 postgres:9.6 "docker-entrypoint.s…" 7 minutes ago Up 7 minutes (healthy) 127.0.0.1:15432->5432/tcp project_postgres_1
93e15c18d879 nginx:mainline "nginx -g 'daemon of…" 7 minutes ago Up 7 minutes (health: starting) 127.0.0.1:80->80/tcp, 127.0.0.1:443->443/tcp nginx

来自 docker inspect nginx 的示例(部分,为简洁起见)输出:
    "State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 11568,
"ExitCode": 0,
"Error": "",
"StartedAt": "2018-02-13T21:04:22.904241169Z",
"FinishedAt": "0001-01-01T00:00:00Z",
"Health": {
"Status": "unhealthy",
"FailingStreak": 0,
"Log": []
}
},
docker-compose.yml的部分定义 nginx容器:
nginx:
image: nginx:mainline
# using container_name means there will only ever be one nginx container!
container_name: nginx
restart: always
networks:
- proxynet
volumes:
- /etc/nginx/conf.d
- /etc/nginx/vhost.d
- /usr/share/nginx/html
- tlsdata:/etc/nginx/certs:ro
- attachdata:/usr/share/nginx/html/uploads:ro
- staticdata:/usr/share/nginx/html/static:ro
- ./nginx/healthcheck.sh:/bin/healthcheck.sh
healthcheck:
test: ['CMD', '/bin/healthcheck.sh']
interval: 1m
timeout: 5s
retries: 3
ports:
# Make the http/https ports available on the Docker host IPv4 loopback interface
- '127.0.0.1:80:80'
- '127.0.0.1:443:443'
healthcheck.sh我作为一个卷加载:
#!/bin/bash

service nginx status || exit 1

看起来问题只是一个问题,当容器最初启动时,systemd 从不从状态检查中返回,同时配置的健康检查超时不会触发。其他一切正常, nginx已启动并响应,但如果每次启动时无需手动重新启动,运行状况检查正常运行会很好。

我的配置中是否缺少某些内容,或者我可以运行更好的检查?

最佳答案

我认为在这种情况下不需要自定义脚本。

尝试将您的健康检查测试更改为

test: ["CMD", "service", "nginx", "status"]

这对我来说很好用。

尝试使用 "而不是 '同样,以防万一:)

编辑

如果你真的想强制一个 exit 1 ,万一失败,你可以使用:
test: service nginx status || exit 1

关于nginx 容器的 Docker 健康检查,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48776044/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com