【问题标题】:Running gunicorn from Dockerfile not working, but works with manual start从 Dockerfile 运行 gunicorn 不起作用,但可以手动启动
【发布时间】:2019-04-03 06:35:09
【问题描述】:

这是我的项目结构-

/app
  /config
  /controllers
  /lib
  /schemas
  /static
  /templates
  /tests
  __init__.py
Dockerfile
wsgi.py

这就是我的 Dockerfile 的样子

FROM python:3.6

WORKDIR /app

COPY ./requirements.txt /.requirements.txt

COPY . /app

# Install any needed packages specified in requirements.txt
RUN pip install -r requirements.txt

EXPOSE 8000

ENV FLASK_APP=iterative.py
ENV FLASK_ENV=development


CMD gunicorn -b 0.0.0.0:8000 -w 4 wsgi:app

这就是我的wsgi.py 的样子

from app import app

print("IS WSGI BEING RUN")
if __name__ == '__main__':
    print("IS THIS RUNNING")
    app.run(host="0.0.0.0", debug=True, threaded=True)

这就是我的__init.py 的样子

from flask import Flask
import sys
app = Flask(__name__)
from app import routes
print("IS INIT.PY RUNNING")

这就是我的工作

  1. 我运行 `docker build -t 。
  2. 然后我运行docker run 80:8000 <app_name>

这是我得到的输出

[2019-04-03 09:52:09 +0530] [8547] [INFO] Starting gunicorn 19.9.0
[2019-04-03 09:52:09 +0530] [8547] [INFO] Listening at: http://0.0.0.0:8000 (8547)
[2019-04-03 09:52:09 +0530] [8547] [INFO] Using worker: sync
[2019-04-03 09:52:09 +0530] [8550] [INFO] Booting worker with pid: 8550
[2019-04-03 09:52:09 +0530] [8551] [INFO] Booting worker with pid: 8551
[2019-04-03 09:52:09 +0530] [8552] [INFO] Booting worker with pid: 8552
[2019-04-03 09:52:09 +0530] [8553] [INFO] Booting worker with pid: 8553

请注意,没有打印语句出来。

然后我通过 ssh 进入 docker 容器

docker exec -it <container_id> /bin/bash

然后我运行(我在任意端口运行它只是为了证明它有效)

gunicorn -b 9000 -w 4 wsgi:app

现在这是我得到的输出

[2019-04-03 06:25:44 +0000] [30] [INFO] Starting gunicorn 19.9.0
[2019-04-03 06:25:44 +0000] [30] [INFO] Listening at: http://0.0.35.40:8000 (30)
[2019-04-03 06:25:44 +0000] [30] [INFO] Using worker: sync
[2019-04-03 06:25:44 +0000] [33] [INFO] Booting worker with pid: 33
[2019-04-03 06:25:44 +0000] [35] [INFO] Booting worker with pid: 35
/usr/local/lib/python3.6/site-packages/psycopg2/__init__.py:144: UserWarning: The psycopg2 wheel package will be renamed from release 2.8; in order to keep installing from binary please use "pip install psycopg2-binary" instead. For details see: <http://initd.org/psycopg/docs/install.html#binary-install-from-pypi>.
  """)
[2019-04-03 06:25:44 +0000] [36] [INFO] Booting worker with pid: 36
/usr/local/lib/python3.6/site-packages/psycopg2/__init__.py:144: UserWarning: The psycopg2 wheel package will be renamed from release 2.8; in order to keep installing from binary please use "pip install psycopg2-binary" instead. For details see: <http://initd.org/psycopg/docs/install.html#binary-install-from-pypi>.
  """)
[2019-04-03 06:25:44 +0000] [38] [INFO] Booting worker with pid: 38
/usr/local/lib/python3.6/site-packages/psycopg2/__init__.py:144: UserWarning: The psycopg2 wheel package will be renamed from release 2.8; in order to keep installing from binary please use "pip install psycopg2-binary" instead. For details see: <http://initd.org/psycopg/docs/install.html#binary-install-from-pypi>.
  """)
FLASK ENVIRONMENT: development
IS INIT.PY RUNNING
IS WSGI BEING RUN
IS WSGI BEING RUN
/usr/local/lib/python3.6/site-packages/psycopg2/__init__.py:144: UserWarning: The psycopg2 wheel package will be renamed from release 2.8; in order to keep installing from binary please use "pip install psycopg2-binary" instead. For details see: <http://initd.org/psycopg/docs/install.html#binary-install-from-pypi>.
  """)
FLASK ENVIRONMENT: development
IS INIT.PY RUNNING
IS WSGI BEING RUN
IS WSGI BEING RUN
FLASK ENVIRONMENT: development
IS INIT.PY RUNNING
IS WSGI BEING RUN
IS WSGI BEING RUN
FLASK ENVIRONMENT: development
IS INIT.PY RUNNING
IS WSGI BEING RUN
IS WSGI BEING RUN

我检查 docker 日志以确保容器的输出不只是写入日志。它不是。

为什么这两种情况的行为不同?

【问题讨论】:

    标签: python docker gunicorn


    【解决方案1】:

    好的,这真的很令人沮丧,我希望这对其他人有所帮助。

    那里的每个教程都没有强调运行 Docker 的操作系统。 Docker 并非在所有操作系统中都以相同的方式运行。

    其实Mac OS,你只能通过localhost打你的容器。

    https://docs.docker.com/docker-for-mac/networking/#known-limitations-use-cases-and-workarounds
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-09-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-27
      • 2021-09-23
      • 1970-01-01
      相关资源
      最近更新 更多