从Dockerfile运行gunicorn无法正常工作,但可以手动启动

问题描述 投票:1回答:1

这是我的项目结构 -

/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个回答
0
投票

好的,这真的令人沮丧,我希望这会帮助其他人。

每个教程都没有强调Docker运行的操作系统。 Docker不会在所有操作系统上运行相同的操作。

事实上,在Mac OS中,你只能通过localhost来打你的容器。

https://docs.docker.com/docker-for-mac/networking/#known-limitations-use-cases-and-workarounds
© www.soinside.com 2019 - 2024. All rights reserved.