简体   繁体   English

在Docker / Docker-Compose上理解Gunicorn和Flask

[英]Understanding Gunicorn and Flask on Docker/Docker-Compose

I'm having trouble getting Flask and Gunicorn to work properly on Docker using Docker-compose 我在使用Docker-compose让Flask和Gunicorn在Docker上正常工作时遇到了麻烦

Dockerfile: Dockerfile:

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: 码头工人,Compose.yml:

version: '2'
services:
 web:
  build: .
  volumes:
  - ./:/var/www/crypto
  ports:
   - "5000:5000"

run.py: run.py:

from app import app
app.run()

From my understanding the Gunicorn master will run at port 8000 on all interfaces in the container 根据我的理解,Gunicorn master将在容器中的所有接口上的端口8000上运行

And then it'll spawn a node to run at port 5000 in the container at 127.0.0.1/localhost. 然后它将生成一个节点,在127.0.0.1/localhost的容器中的端口5000上运行。

From there I link port 5000 in the container to my host port 8000 从那里我将container端口5000链接到我的host端口8000

I expected to see my application from my host computer at http://127.0.0.1:8000 instead nothing happened and nothing seemed to be connecting. 我希望从我的主机http://127.0.0.1:8000看到我的应用程序,而不是什么都没发生,似乎没有任何连接。

I have done this before but can't remember what I did differently. 我以前做过这个,但不记得我做了什么不同。

(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)

^the reason why is because it seems like it spawned a worker and is running it at port 5000, i can't access my app through port 8000 ^原因是因为它似乎产生了一个工人并在端口5000运行它,我无法通过端口8000访问我的应用程序

app.run() and gunicorn are two ways to run a webserver. app.run()gunicorn是运行网络服务器的两种方式。 The first is the Flask development server, and it's useful for development but shouldn't be deployed in production. 第一个是Flask开发服务器,它对开发很有用,但不应该在生产中部署。 You shouldn't run both at the same time. 你不应该同时运行这两个。

gunicorn should be pointed to the app object so that it can import it and use it to run the webserver itself. gunicorn应该指向app对象,以便它可以导入它并使用它来运行web服务器本身。 That's all it needs. 这就是它所需要的一切。

Instead of CMD [ "gunicorn", "-b", ":8000", "run" ] 而不是CMD [ "gunicorn", "-b", ":8000", "run" ]

Do CMD ["gunicorn", "app:app", "-b", "0.0.0.0:8000"] CMD ["gunicorn", "app:app", "-b", "0.0.0.0:8000"]

You can see that instead of the telling the gunicorn process to run you instead tell the process where to look . 你可以看到,而不是告诉gunicorn进程run你,而不是告诉进程在哪里 The application that you want gunicorn to serve is app . 你想要gunicorn服务的appapp You can also add more options to the gunicorn command such as reload , the number of workers, timeout, log-levels, etc... 您还可以为gunicorn命令添加更多选项,例如reload ,工作者数量,超时,日志级别等...

To expand on Alex Hall's answer, you don't want to run a Flask server on production, because the ability to scale is very limited. 为了扩展Alex Hall的答案,您不希望在生产中运行Flask服务器,因为扩展能力非常有限。 According to the Flask docs, the mention that: 根据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 Flask的内置服务器不适合生产,因为它不能很好地扩展,默认情况下一次只能提供一个请求

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

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