Docker 健康检查失败时容器不重启

Docker container not restarting on health check failures

我有以下 docker-compose 文件

version: "3.7"
services:
  myservice:
    restart: always
    image: myimage
    hostname: myhost
    env_file:
      - ./env/common.env
    healthcheck:
      test: ["CMD-SHELL", "curl --silent --fail localhost:9091/status || exit 1"]
      interval: 5s
      timeout: 1s
      retries: 1
      start_period: 5s   
    ports:
      - 27018:27018
    volumes: 
      - type: bind
        source: ./scripts/myservice
        target: /scripts  
    entrypoint: ["/bin/sh", "-c", "apk add bash && chmod a+x /scripts/init.sh && /scripts/init.sh"]

在 运行 宁 docker-compose run myservice,我的容器正确启动(它没有崩溃)。 但是,健康检查失败,当我执行 docker ps 时,我可以看到容器不健康。

我希望容器在容器运行状况不佳时自动重启,但是容器在日志中抛出错误并在进行 curl 调用时继续 运行。

如何确保在健康检查失败时重启我的容器?

你可能想看看this,它的功能如下:

Monitor and restart unhealthy docker containers. This functionality was proposed to be included with the addition of HEALTHCHECK, however didn't make the cut. This container is a stand-in till there is native support for --exit-on-unhealthy https://github.com/docker/docker/pull/22719.

对于您的情况,您可以像下一步一样设置一个具有上述支持的监控容器,该容器可以监控所有不健康的容器并为您重启它们。

docker run -d \
    --name autoheal \
    --restart=always \
    -e AUTOHEAL_CONTAINER_LABEL=all \
    -v /var/run/docker.sock:/var/run/docker.sock \
    willfarrell/autoheal