Docker:运行 通过 Gunicorn 的 Flask 应用程序 - 工人超时?表现不佳?
Docker: Running a Flask app via Gunicorn - Worker timeouts? Poor performance?
我正在尝试创建一个用 Python Flask 编写的新应用程序,运行 由 gunicorn 然后 dockerised。
我遇到的问题是 docker 容器内的性能非常差,不一致,我最终得到了响应,但我不明白为什么性能会下降。有时我会在日志中看到 [CRITICAL] WORKER TIMEOUT (pid:9)
.
这是我的应用程序:
server.py
from flask import Flask
app = Flask(__name__)
@app.route('/')
def index():
return "The server is running!"
if __name__ == '__main__':
app.run()
Dockerfile
FROM python:3.6.9-slim
# Copy all the files to the src folder
COPY build/ /usr/src/
# Create the virtual environment
RUN python3 -m venv /usr/src/myapp_venv
# Install the requirements
RUN /usr/src/myapp_venv/bin/pip3 install -r /usr/src/requirements.txt
# Runs gunicorn
# --chdir sets the directory where gunicorn should look for the server files
# server:app means run the "server.py" file and look for the "app" constructor within that
ENTRYPOINT ["/usr/src/myapp/bin/gunicorn", "--bind", "0.0.0.0:5000", "--workers", "1", "--chdir", "/usr/src/", "server:app"]
# Expose the gunicorn port
EXPOSE 5000
requirements.txt
Click==7.0
Flask==1.1.1
gunicorn==20.0.0
itsdangerous==1.1.0
Jinja2==2.10.3
MarkupSafe==1.1.1
Werkzeug==0.16.0
我 运行 docker 容器是这样的:
docker build -t killerkode/myapp .
docker run --name myapp -p 5000:5000 killerkode/myapp
我设法找到了这篇有用的文章,它解释了 Gunicorn 挂起的原因。
https://pythonspeed.com/articles/gunicorn-in-docker/
我的解决方案是更改工作人员临时目录并将最少工作人员增加到 2 个。我仍然看到工作人员被杀死,但不再有任何延迟/缓慢。我怀疑添加 gthread
会进一步改善。
这是我更新的 Dockerfile:
FROM python:3.6.9-slim
# Copy all the files to the src folder
COPY build/ /usr/src/
# Create the virtual environment
RUN python3 -m venv /usr/src/myapp_venv
# Install the requirements
RUN /usr/src/myapp_venv/bin/pip3 install -r /usr/src/requirements.txt
# Runs gunicorn
# --chdir sets the directory where gunicorn should look for the server files
# server:app means run the "server.py" file and look for the "app" constructor within that
ENTRYPOINT ["/usr/src/myapp/bin/gunicorn", "--bind", "0.0.0.0:5000", "--worker-tmp-dir", "/dev/shm", "--workers", "2", "--chdir", "/usr/src/", "server:app"]
# Expose the gunicorn port
EXPOSE 5000
我正在尝试创建一个用 Python Flask 编写的新应用程序,运行 由 gunicorn 然后 dockerised。
我遇到的问题是 docker 容器内的性能非常差,不一致,我最终得到了响应,但我不明白为什么性能会下降。有时我会在日志中看到 [CRITICAL] WORKER TIMEOUT (pid:9)
.
这是我的应用程序:
server.py
from flask import Flask
app = Flask(__name__)
@app.route('/')
def index():
return "The server is running!"
if __name__ == '__main__':
app.run()
Dockerfile
FROM python:3.6.9-slim
# Copy all the files to the src folder
COPY build/ /usr/src/
# Create the virtual environment
RUN python3 -m venv /usr/src/myapp_venv
# Install the requirements
RUN /usr/src/myapp_venv/bin/pip3 install -r /usr/src/requirements.txt
# Runs gunicorn
# --chdir sets the directory where gunicorn should look for the server files
# server:app means run the "server.py" file and look for the "app" constructor within that
ENTRYPOINT ["/usr/src/myapp/bin/gunicorn", "--bind", "0.0.0.0:5000", "--workers", "1", "--chdir", "/usr/src/", "server:app"]
# Expose the gunicorn port
EXPOSE 5000
requirements.txt
Click==7.0
Flask==1.1.1
gunicorn==20.0.0
itsdangerous==1.1.0
Jinja2==2.10.3
MarkupSafe==1.1.1
Werkzeug==0.16.0
我 运行 docker 容器是这样的:
docker build -t killerkode/myapp .
docker run --name myapp -p 5000:5000 killerkode/myapp
我设法找到了这篇有用的文章,它解释了 Gunicorn 挂起的原因。 https://pythonspeed.com/articles/gunicorn-in-docker/
我的解决方案是更改工作人员临时目录并将最少工作人员增加到 2 个。我仍然看到工作人员被杀死,但不再有任何延迟/缓慢。我怀疑添加 gthread
会进一步改善。
这是我更新的 Dockerfile:
FROM python:3.6.9-slim
# Copy all the files to the src folder
COPY build/ /usr/src/
# Create the virtual environment
RUN python3 -m venv /usr/src/myapp_venv
# Install the requirements
RUN /usr/src/myapp_venv/bin/pip3 install -r /usr/src/requirements.txt
# Runs gunicorn
# --chdir sets the directory where gunicorn should look for the server files
# server:app means run the "server.py" file and look for the "app" constructor within that
ENTRYPOINT ["/usr/src/myapp/bin/gunicorn", "--bind", "0.0.0.0:5000", "--worker-tmp-dir", "/dev/shm", "--workers", "2", "--chdir", "/usr/src/", "server:app"]
# Expose the gunicorn port
EXPOSE 5000