Flask with gunicorn 问题与 wsgi

Flask with gunicorn problems with wsgi

我正在尝试使用 gunicorn 为 Flask 应用程序设置 dockerized 生产环境。我按照这个 Digital Ocean's instructions together with testdriven's one 进行 dockerizing。

项目结构如下:

tree -L 2
.
├── Docker
│   ├── Dockerfile
│   ├── Dockerfile-nginx
│   └── nginx.conf
├── dev-requirements.txt
├── docker-compose.prod.yml
├── docker-compose.yml
├── gunicorn_conf.py
├── requirements.txt
├── setup.cfg
├── src
│   ├── __pycache__
│   ├── config.py
│   ├── main.py
│   ├── models.py
│   ├── tests.py
│   ├── views.py
│   └── wsgi.py
└── venv
    ├── bin
    ├── include
    ├── lib
    └── pip-selfcheck.json

7 directories, 16 files

配置位于 docker-compose.prod.yml:

version: "3.7"

services:
  web:
    build:
      context: .
      dockerfile: Docker/Dockerfile
    env_file:
      - .web.env
    ports:
      - "5000:5000"
    depends_on:
      - db
    command: gunicorn wsgi:app -c ../gunicorn_conf.py
    working_dir: /app/src
  db:
    image: "postgres:11"
    volumes:
      - simple_app_data:/var/lib/postgresql/data
    env_file:
      - .db.env
volumes:
  simple_app_data:

gunicorn_conf.py 的内容:

bind = "0.0.0.0:5000"
workers = 2

wsgi.py

from main import app

print('*'*10)
print(__name__)
print('*'*10+'\n')

if __name__ == '__main__':
    app.run()

当我尝试使用 docker-compose -f docker-compose.prod.yml build --force-rm --no-cache web && docker-compose -f docker-compose.prod.yml run web 运行 此配置时,我得到以下日志:

Starting simple_app_db_1 ... done
[2019-12-18 12:15:45 +0000] [1] [INFO] Starting gunicorn 20.0.4
[2019-12-18 12:15:45 +0000] [1] [INFO] Listening at: http://0.0.0.0:5000 (1)
[2019-12-18 12:15:45 +0000] [1] [INFO] Using worker: sync
[2019-12-18 12:15:45 +0000] [9] [INFO] Booting worker with pid: 9
[2019-12-18 12:15:45 +0000] [10] [INFO] Booting worker with pid: 10
/usr/local/lib/python3.7/site-packages/flask_sqlalchemy/__init__.py:835: FSADeprecationWarning: SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and will be disabled by default in the future.  Set it to True or False to suppress this warning.
  'SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and '
**********
wsgi
**********

/usr/local/lib/python3.7/site-packages/flask_sqlalchemy/__init__.py:835: FSADeprecationWarning: SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and will be disabled by default in the future.  Set it to True or False to suppress this warning.
  'SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and '
**********
wsgi
**********

所以 wsgi.py 文件不是 __main__。但是,当我试图摆脱这个 if:

from main import app

print('*'*10)
print(__name__)
print('*'*10+'\n')

app.run()

我得到:

OSError: [Errno 98] Address already in use

如何更正此配置以使用 gunicorn?

在命令中使用gunicorn用于运行 docker,即:

# Dockerfile
...
CMD [ "gunicorn", "-w3", "--bind=0.0.0.0:9999", "wsgi" ]

如果您使用入口点,只需在入口点脚本末尾添加此命令。

您可能需要进行一些试验才能找到正确的命令。语法如下:

<folder>.<filename>:<python_object>

您的 wsgi.py 文件可能如下所示:

# wsgi.py

from main import app as application  # what gunicorn expects
# or
from main import app  # then use gunicorn wsgi:app 

随时查看我的 repo here 以获得一个有效的示例

原来docker不知为何没有暴露端口

 → docker-compose ps
             Name                            Command               State    Ports
-----------------------------------------------------------------------------------
simple_app_db_1                   docker-entrypoint.sh postgres    Up      5432/tcp
simple_app_web_run_a7165f8215b2   gunicorn wsgi:app -c ../gu ...   Up
 → docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
6c0f5f884135        simple_app_web      "gunicorn wsgi:app -…"   59 seconds ago      Up 57 seconds                           simple_app_web_run_a7165f8215b2
e2948b152b36        postgres:11         "docker-entrypoint.s…"   38 minutes ago      Up 38 minutes       5432/tcp            simple_app_db_1

当我更改用于 运行 应用程序的命令时,一切正常:

docker-compose -f docker-compose.prod.yml run --publish 5000:5000 web