Docker 容器中的 Gunicorn Flask 应用程序未公开
Gunicorn Flask application in Docker Container not getting Exposed
容器内的应用程序无法从外部访问,即如果我执行到 docker 容器并执行
curl localhost:5000
它可以正常工作,但不能在我电脑的浏览器上运行我收到错误消息:无法访问此站点
我的 Dockerfile:
# Use an official Python runtime as a parent image
FROM python:3.7-slim
# Set the working directory to /app
WORKDIR /web-engine
# Copy the current directory contents into the container at /app
COPY . /web-engine
# Install Gunicorn3
RUN apt-get update && apt-get install default-libmysqlclient-dev gcc -y
# Install any needed packages specified in requirements.txt
RUN pip3 install --trusted-host pypi.python.org -r requirements.txt
# Make port 5000 available to the world outside this container
EXPOSE 5000
# Define environment variable
ENV username root
# Run app.py when the container launches
CMD gunicorn --workers 4 --bind 127.0.0.1:5000 application:app --threads 1
以这种方式执行 docker 后:
sudo docker run -e password=$password -p 5000:5000 $reg/web-engine:ve0.0.2
我得到以下输出:
[2019-09-08 11:53:36 +0000] [6] [INFO] Starting gunicorn 19.9.0
[2019-09-08 11:53:36 +0000] [6] [INFO] Listening at: http://127.0.0.1:5000 (6)
[2019-09-08 11:53:36 +0000] [6] [INFO] Using worker: sync
[2019-09-08 11:53:36 +0000] [9] [INFO] Booting worker with pid: 9
[2019-09-08 11:53:36 +0000] [10] [INFO] Booting worker with pid: 10
[2019-09-08 11:53:36 +0000] [11] [INFO] Booting worker with pid: 11
[2019-09-08 11:53:36 +0000] [12] [INFO] Booting worker with pid: 12
如您所见,我正在将容器的端口 5000 映射到我计算机的端口 5000,但是 localhost:5000 不工作
因此我尝试了所有相同的方法,但是使用了 Flask 的开发服务器
在我的 docker 文件
中进行以下更改
FRom
CMD gunicorn --workers 4 --bind 127.0.0.1:5000 application:app --threads 1
到
CMD python3.7 application.py
它成功了;我转到 localhost:5000 并看到应用程序正在运行
申请没有问题。我想 gunicorn 服务器有错误
requirements.txt 文件:
Flask
Flask-SQLAlchemy
mysqlclient
gunicorn
bs4
html5lib
请帮帮我
我还尝试了不同形式的 gunicorn 和 docker 运行 命令组合,例如
CMD gunicorn -application:app && sudo docker run -e password=$password -p 5000:8000 $reg/web-engine:ve0.0.2
没用
terminal image of container working with development server
我希望能有一个解决方案不涉及此处提到的内容,例如 nginx、supervisor 等
有人请 helllppp meeeeee.......
默认情况下,127.0.0.1 在容器内的含义与在容器外的含义不同。请改用 0.0.0.0:5000
。
扩展另一个答案:Docker 端口转发的工作方式是从主机上的 IP 转发到容器中的 one 地址。具体来说,与将容器连接到主机网络的 Docker 桥接网络通信的外部地址。它无法与容器上的 127.0.0.1
通信,因为那是私有 IP——主机有自己的 127.0.0.1
.
因此您需要绑定到那个 Docker 桥接网络,或者通过绑定到 0.0.0.0
来绑定到所有接口,这是最简单的解决方案。
用散文来解释这个有点棘手,但我有更长的版本,还包括图表,因此可能更容易理解:https://pythonspeed.com/articles/docker-connection-refused/
在您的 Dockerfile 中尝试此命令:
CMD ["flask", "run", "--host=0.0.0.0"]
容器内的应用程序无法从外部访问,即如果我执行到 docker 容器并执行
curl localhost:5000
它可以正常工作,但不能在我电脑的浏览器上运行我收到错误消息:无法访问此站点
我的 Dockerfile:
# Use an official Python runtime as a parent image
FROM python:3.7-slim
# Set the working directory to /app
WORKDIR /web-engine
# Copy the current directory contents into the container at /app
COPY . /web-engine
# Install Gunicorn3
RUN apt-get update && apt-get install default-libmysqlclient-dev gcc -y
# Install any needed packages specified in requirements.txt
RUN pip3 install --trusted-host pypi.python.org -r requirements.txt
# Make port 5000 available to the world outside this container
EXPOSE 5000
# Define environment variable
ENV username root
# Run app.py when the container launches
CMD gunicorn --workers 4 --bind 127.0.0.1:5000 application:app --threads 1
以这种方式执行 docker 后:
sudo docker run -e password=$password -p 5000:5000 $reg/web-engine:ve0.0.2
我得到以下输出:
[2019-09-08 11:53:36 +0000] [6] [INFO] Starting gunicorn 19.9.0
[2019-09-08 11:53:36 +0000] [6] [INFO] Listening at: http://127.0.0.1:5000 (6)
[2019-09-08 11:53:36 +0000] [6] [INFO] Using worker: sync
[2019-09-08 11:53:36 +0000] [9] [INFO] Booting worker with pid: 9
[2019-09-08 11:53:36 +0000] [10] [INFO] Booting worker with pid: 10
[2019-09-08 11:53:36 +0000] [11] [INFO] Booting worker with pid: 11
[2019-09-08 11:53:36 +0000] [12] [INFO] Booting worker with pid: 12
如您所见,我正在将容器的端口 5000 映射到我计算机的端口 5000,但是 localhost:5000 不工作
因此我尝试了所有相同的方法,但是使用了 Flask 的开发服务器 在我的 docker 文件
中进行以下更改FRom
CMD gunicorn --workers 4 --bind 127.0.0.1:5000 application:app --threads 1
到
CMD python3.7 application.py
它成功了;我转到 localhost:5000 并看到应用程序正在运行
申请没有问题。我想 gunicorn 服务器有错误
requirements.txt 文件:
Flask
Flask-SQLAlchemy
mysqlclient
gunicorn
bs4
html5lib
请帮帮我
我还尝试了不同形式的 gunicorn 和 docker 运行 命令组合,例如
CMD gunicorn -application:app && sudo docker run -e password=$password -p 5000:8000 $reg/web-engine:ve0.0.2
没用 terminal image of container working with development server
我希望能有一个解决方案不涉及此处提到的内容,例如 nginx、supervisor 等 有人请 helllppp meeeeee.......
默认情况下,127.0.0.1 在容器内的含义与在容器外的含义不同。请改用 0.0.0.0:5000
。
扩展另一个答案:Docker 端口转发的工作方式是从主机上的 IP 转发到容器中的 one 地址。具体来说,与将容器连接到主机网络的 Docker 桥接网络通信的外部地址。它无法与容器上的 127.0.0.1
通信,因为那是私有 IP——主机有自己的 127.0.0.1
.
因此您需要绑定到那个 Docker 桥接网络,或者通过绑定到 0.0.0.0
来绑定到所有接口,这是最简单的解决方案。
用散文来解释这个有点棘手,但我有更长的版本,还包括图表,因此可能更容易理解:https://pythonspeed.com/articles/docker-connection-refused/
在您的 Dockerfile 中尝试此命令:
CMD ["flask", "run", "--host=0.0.0.0"]