【问题标题】:Docker container not restarting on health check failuresDocker 容器未在运行状况检查失败时重新启动
【发布时间】:2019-09-06 01:42:02
【问题描述】:

我有以下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 调用时继续运行。

如何确保我的容器在运行状况检查失败时重新启动?

【问题讨论】:

    标签: docker docker-compose health-monitoring


    【解决方案1】:

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

    监控并重新启动不健康的 docker 容器。建议将此功能包含在添加的 HEALTHCHECK 中,但没有成功。在原生支持 --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
    

    【讨论】:

    • 好的,看起来它只会在基于健康检查的集群模式下重新启动。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-12
    • 2019-06-23
    • 1970-01-01
    相关资源
    最近更新 更多