我发现当我使用 docker-compose 关闭我的Gunicorn (19.7.1) python 应用程序时,总是需要 10 秒才能关闭。这是 docker-compose 在强制杀死进程之前等待的默认最大时间(通过-t / --timeout参数调整)。我认为这意味着gunicorn 没有被优雅地关闭。我可以用以下方法重现这个:
docker-compose.yml:
version: "3"
services:
test:
build: ./
ports:
- 8000:8000
Run Code Online (Sandbox Code Playgroud)
Dockerfile:
FROM python
RUN pip install gunicorn
COPY test.py .
EXPOSE 8000
CMD gunicorn -b :8000 test:app
Run Code Online (Sandbox Code Playgroud)
测试.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])
Run Code Online (Sandbox Code Playgroud)
然后运行应用程序:
docker-compose up -d
Run Code Online (Sandbox Code Playgroud)
并优雅地停止它:
docker-compose stop
Run Code Online (Sandbox Code Playgroud)
版本:
docker-compose …Run Code Online (Sandbox Code Playgroud)