【问题标题】:Restart Docker Container when Gunicorn BreaksGunicorn 中断时重启 Docker 容器
【发布时间】:2018-04-02 07:35:50
【问题描述】:

考虑一个简单的docker-compose.yml,看起来像这样:

version: "3"
services:

  api:
      image: my-container:latest
      command: ["gunicorn", "--bind", "0.0.0.0:8000", "wsgi:app"]
      volumes:
          - ./api:/api

api 服务是一个基于nginx 的 Python Flask Web 应用程序,运行gunicorn。有时,我会破坏 Flask 应用程序,gunicorn 会抛出一个非零退出代码并停止运行。然后我重建我所有的容器。我尝试了以下方法在失败后重新启动容器:

version: "3"
services:

  api:
      image: my-container:latest
      command: ["gunicorn", "--bind", "0.0.0.0:8000", "wsgi:app"]
      volumes:
          - ./api:/api
      deploy:
          restart_policy:
              condition: on-failure
              delay: 5s
              max_attempts: 5
              window: 60s

此配置忽略 deploy 配置选项并显示以下警告:WARNING: Some services (api) use the 'deploy' key, which will be ignored. Compose does not support 'deploy' configuration - usedocker stack deployto deploy to a swarm. I'm not deploying to a swarm。

如何在失败且退出代码非零时自动重新启动容器?

【问题讨论】:

    标签: python-3.x docker flask docker-compose gunicorn


    【解决方案1】:

    deploy 部分仅在使用docker stack deploy 部署到集群时生效,并被docker-compose updocker-compose run 忽略。

    restart 部分仅在使用docker-compose updocker-compose run 时生效。

    version: "3"
    services:
    
      api:
          image: my-container:latest
          command: ["gunicorn", "--bind", "0.0.0.0:8000", "wsgi:app"]
          volumes:
              - ./api:/api
          restart: no|always|on-failure|unless-stopped
    

    请参阅文档:https://docs.docker.com/compose/compose-file/#restart

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-24
      • 2023-02-11
      • 2016-09-25
      • 1970-01-01
      • 2021-04-09
      • 2020-09-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多