当Gunicorn休息时重启Docker容器

时间:2018-04-02 07:35:51

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

考虑一个看起来像这样的简单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网络应用,运行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 - use docker stack deploy to deploy to a swarm.我没有部署到群组。

如果使用非零退出代码失败,如何自动重启容器?

1 个答案:

答案 0 :(得分:0)

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