简体   繁体   English

Gunicorn优雅地停靠与码头工人组成

[英]Gunicorn graceful stopping with docker-compose

I find that when I use docker-compose to shut down my gunicorn (19.7.1) python application, it always takes 10s to shut down. 我发现当我使用docker -compose关闭我的gunicorn (19.7.1)python应用程序时,它总是需要10秒才能关闭。 This is the default maximum time docker-compose waits before forcefully killing the process (adjusted with the -t / --timeout parameter). 这是在强制终止进程之前docker-compose等待的默认最长时间(使用-t / --timeout参数调整)。 I assume this means that gunicorn isn't being gracefully shut down. 我认为这意味着gunicorn没有被优雅地关闭。 I can reproduce this with: 我可以用以下方法重现:

docker-compose.yml: 泊坞窗,compose.yml:

version: "3"
services:
  test:
    build: ./
    ports:
      - 8000:8000

Dockerfile: Dockerfile:

FROM python

RUN pip install gunicorn

COPY test.py .

EXPOSE 8000
CMD gunicorn -b :8000 test:app

test.py test.py

def app(_, start_response):
    """Simplest possible application object"""
    data = b'Hello, World!\n'
    status = '200 OK'
    response_headers = [
        ('Content-type', 'text/plain'),
        ('Content-Length', str(len(data)))
    ]
    start_response(status, response_headers)
    return iter([data])

Then running the app with: 然后运行应用程序:

docker-compose up -d

and gracefully stopping it with: 并优雅地停止它:

docker-compose stop

version: 版:

docker-compose version 1.12.0, build b31ff33

I would prefer to allow gunicorn to stop gracefully. 我宁愿让gunicorn优雅地停下来。 I think it should be able to based on the signal handlers in base.py . 我认为它应该能够基于base.py中的信号处理程序。

All of the above is also true for updating images using docker-compose up -d twice, the second time with a new image to replace the old one. 所有上述情况也适用于使用docker-compose up -d两次更新图像,第二次使用新图像替换旧图像。

Am I misunderstanding / misusing something? 我误解/误用了什么吗? What signal does docker-compose send to stop processes? docker-compose发送什么信号来停止进程? Shouldn't gunicorn be using it? 难道不应该使用它吗? Should I be able to restart my application faster than 10s? 我应该能够以超过10秒的速度重启我的应用程序吗?

TL;DR TL; DR

Add exec after CMD in your dockerfile: CMD exec gunicorn -b :8000 test:app . 添加exec CMD后,在您的dockerfile: CMD exec gunicorn -b :8000 test:app

Details 细节

I had the same issue, when I ran docker exec my_running_gunicorn ps aux , I saw something like: 我有同样的问题,当我运行docker exec my_running_gunicorn ps aux ,我看到类似的东西:

gunicorn     1  0.0  0.0   4336   732 ?        Ss   10:38   0:00 /bin/sh -c gunicorn -c gunicorn.conf.py vision:app
gunicorn     5  0.1  1.1  91600 22636 ?        S    10:38   0:00 /usr/local/bin/python /usr/local/bin/gunicorn -c gunicorn.conf.py vision:app
gunicorn     8  0.2  2.5 186328 52540 ?        S    10:38   0:00 /usr/local/bin/python /usr/local/bin/gunicorn -c gunicorn.conf.py vision:app

The 1 PID is not the gunicorn master, hence it didn't receive the sigterm signal. 1 PID不是gunicorn主机,因此它没有收到sigterm信号。

With the exec in the Dockerfile, I now have 有了Dockerfile中的exec ,我现在有了

gunicorn     1 32.0  1.1  91472 22624 ?        Ss   10:43   0:00 /usr/local/bin/python /usr/local/bin/gunicorn -c gunicorn.conf.py vision:app
gunicorn     7 45.0  1.9 131664 39116 ?        R    10:43   0:00 /usr/local/bin/python /usr/local/bin/gunicorn -c gunicorn.conf.py vision:app

and it works. 它的工作原理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM