簡體   English   中英

帶有 wsgi 的 gunicorn 問題的燒瓶

[英]Flask with gunicorn problems with wsgi

我正在嘗試使用 gunicorn 為 Flask 應用程序設置 dockerized 生產環境。 我按照這個Digital Ocean 的說明和testdriven 的說明一起進行 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?

在用於運行 docker 的命令中使用 gunicorn,即:

# 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 

隨意在這里檢查我的回購以獲得一個有效的例子

事實證明,由於某種原因,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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM