Gunicorn优雅地停靠与docker构成
我发现,当我使用docker -composeclosures我的gunicorn (19.7.1)python应用程序时,总是需要10sclosures。 这是docker-compose在强制终止进程之前等待的默认最长时间(使用-t / --timeout
参数进行调整)。 我认为这意味着gunicorn没有被优雅地closures。 我可以重现这一点:
泊坞窗,compose.yml:
version: "3" services: test: build: ./ ports: - 8000:8000
Dockerfile:
FROM python RUN pip install gunicorn COPY test.py . EXPOSE 8000 CMD gunicorn -b :8000 test:app
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])
然后运行该应用程序:
docker-compose up -d
并优雅地阻止它:
docker-compose stop
版:
docker-compose version 1.12.0, build b31ff33
我宁愿让gunicorn优雅地停下来。 我认为它应该能够基于base.py中的信号处理程序。
以上所有情况也适用于使用docker-compose up -d
更新图像两次,第二次用新图像replace旧图像。
我误解/误用了什么? docker-compose发送什么信号来停止进程? 不应该gunicorn使用它? 我应该能够重新启动我的应用程序比10几秒?
TL; DR
在你的dockerfile中的CMD exec gunicorn -b :8000 test:app
之后添加exec
: CMD exec gunicorn -b :8000 test:app
。
细节
我遇到同样的问题,当我运行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
1 PID不是gunicorn主机,因此它没有收到sigterm信号。
用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
它的工作。