在 Docker/Docker-Compose 上了解 Gunicorn 和 Flask
Understanding Gunicorn and Flask on Docker/Docker-Compose
我无法使用 Docker-compose
让 Flask 和 Gunicorn 在 Docker 上正常工作
Docker文件:
FROM ubuntu:latest
MAINTAINER Kyle Calica "Kyle Calica"
RUN apt-get update -y
RUN apt-get install -y python3-dev build-essential python-pip gunicorn
RUN pip install --upgrade setuptools
RUN pip install ez_setup
COPY . /app
WORKDIR /app
RUN pip install -r ./app/requirements.txt
CMD [ "gunicorn", "-b", ":8000", "run" ]
Docker-Compose.yml:
version: '2'
services:
web:
build: .
volumes:
- ./:/var/www/crypto
ports:
- "5000:5000"
run.py:
from app import app
app.run()
根据我的理解,Gunicorn master 将 运行 在 container
中所有接口上的端口 8000
然后它将在 127.0.0.1/localhost 的 容器 的端口 5000 中生成一个节点到 运行。
从那里我 container
中的 link 端口 5000 到我的 host
端口 8000
我希望在 http://127.0.0.1:8000
的主机上看到我的应用程序
相反,什么也没发生,似乎也没有什么联系。
我以前做过,但不记得我做了什么不同的事情。
(env) paper-street:CoinSlack kyle$ gunicorn -b :8000 run
[2017-09-16 17:43:59 -0700] [15402] [INFO] Starting gunicorn 19.7.1
[2017-09-16 17:43:59 -0700] [15402] [INFO] Listening at: http://0.0.0.0:8000 (15402)
[2017-09-16 17:43:59 -0700] [15402] [INFO] Using worker: sync
[2017-09-16 17:43:59 -0700] [15405] [INFO] Booting worker with pid: 15405
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
^原因是因为它似乎产生了一个工人并且运行在端口 5000 上连接它,我无法通过端口 8000 访问我的应用程序
app.run()
和 gunicorn
是 运行 网络服务器的两种方式。第一个是 Flask 开发服务器,它对开发很有用,但不应该部署在生产环境中。你不应该同时运行。
gunicorn
应指向 app
对象,以便它可以导入它并将其用于 运行 网络服务器本身。这就是它所需要的。
而不是CMD [ "gunicorn", "-b", ":8000", "run" ]
做CMD ["gunicorn", "app:app", "-b", "0.0.0.0:8000"]
您可以看到,您没有告诉 gunicorn 进程run
,而是告诉进程查找 的位置。您希望 gunicorn 服务的应用程序是 app
。您还可以向 gunicorn 命令添加更多选项,例如 reload
、工作人员数量、超时、日志级别等...
为了扩展 Alex Hall 的回答,您不想 运行 在生产环境中使用 Flask 服务器,因为扩展能力非常有限。根据 Flask 文档,提到:
Flask’s built-in server is not suitable for production as it doesn’t
scale well and by default serves only one request at a time
我无法使用 Docker-compose
让 Flask 和 Gunicorn 在 Docker 上正常工作Docker文件:
FROM ubuntu:latest
MAINTAINER Kyle Calica "Kyle Calica"
RUN apt-get update -y
RUN apt-get install -y python3-dev build-essential python-pip gunicorn
RUN pip install --upgrade setuptools
RUN pip install ez_setup
COPY . /app
WORKDIR /app
RUN pip install -r ./app/requirements.txt
CMD [ "gunicorn", "-b", ":8000", "run" ]
Docker-Compose.yml:
version: '2'
services:
web:
build: .
volumes:
- ./:/var/www/crypto
ports:
- "5000:5000"
run.py:
from app import app
app.run()
根据我的理解,Gunicorn master 将 运行 在 container
中所有接口上的端口 8000然后它将在 127.0.0.1/localhost 的 容器 的端口 5000 中生成一个节点到 运行。
从那里我 container
中的 link 端口 5000 到我的 host
端口 8000
我希望在 http://127.0.0.1:8000
的主机上看到我的应用程序
相反,什么也没发生,似乎也没有什么联系。
我以前做过,但不记得我做了什么不同的事情。
(env) paper-street:CoinSlack kyle$ gunicorn -b :8000 run
[2017-09-16 17:43:59 -0700] [15402] [INFO] Starting gunicorn 19.7.1
[2017-09-16 17:43:59 -0700] [15402] [INFO] Listening at: http://0.0.0.0:8000 (15402)
[2017-09-16 17:43:59 -0700] [15402] [INFO] Using worker: sync
[2017-09-16 17:43:59 -0700] [15405] [INFO] Booting worker with pid: 15405
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
^原因是因为它似乎产生了一个工人并且运行在端口 5000 上连接它,我无法通过端口 8000 访问我的应用程序
app.run()
和 gunicorn
是 运行 网络服务器的两种方式。第一个是 Flask 开发服务器,它对开发很有用,但不应该部署在生产环境中。你不应该同时运行。
gunicorn
应指向 app
对象,以便它可以导入它并将其用于 运行 网络服务器本身。这就是它所需要的。
而不是CMD [ "gunicorn", "-b", ":8000", "run" ]
做CMD ["gunicorn", "app:app", "-b", "0.0.0.0:8000"]
您可以看到,您没有告诉 gunicorn 进程run
,而是告诉进程查找 的位置。您希望 gunicorn 服务的应用程序是 app
。您还可以向 gunicorn 命令添加更多选项,例如 reload
、工作人员数量、超时、日志级别等...
为了扩展 Alex Hall 的回答,您不想 运行 在生产环境中使用 Flask 服务器,因为扩展能力非常有限。根据 Flask 文档,提到:
Flask’s built-in server is not suitable for production as it doesn’t scale well and by default serves only one request at a time