这是我的项目结构-
/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")
这就是我所做的
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日志以确保容器的输出不只是被写入日志。它不是。
为什么两种情况下的行为不同?
答案 0 :(得分:0)
好的,这确实令人沮丧,我希望这会帮助其他人。
那里的每个教程都没有强调Docker运行的操作系统。 Docker在所有操作系统上的运行方式都不相同。
实际上,在Mac OS中,您只能通过localhost来访问容器。
https://docs.docker.com/docker-for-mac/networking/#known-limitations-use-cases-and-workarounds