Docker Container中的Gunicorn Flask应用程序未暴露

时间:2019-09-08 14:15:36

标签: python docker flask kubernetes gunicorn

无法从外部访问容器内部的应用程序,即,如果我执行docker容器并执行

Windows.IO

它可以正常工作,但是不能在我的计算机上的浏览器上出现错误:无法访问该网站

我的Dockerfile:

curl localhost:5000 

UPON以这种方式执行docker:

# 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

我得到以下输出:

sudo docker run -e password=$password -p 5000:5000 $reg/web-engine:ve0.0.2

因此您可以看到我正在将容器的端口5000映射到计算机的端口5000,但是localhost:5000无法正常工作

因此,我尝试使用Flask的开发服务器进行相同的操作 在我的dockerfile中进行了以下更改

FRom

[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

CMD gunicorn --workers 4 --bind 127.0.0.1:5000 application:app --threads 1

和它的工作;我转到localhost:5000并看到应用程序正在运行

该应用程序没有任何问题。我想gunicorn服务器出现错误

requirements.txt文件:

CMD python3.7 application.py

请帮帮我

我还尝试了不同形式的gunicorn和docker run命令组合

Flask
Flask-SQLAlchemy
mysqlclient
gunicorn
bs4
html5lib

它没有用 terminal image of container working with development server

我希望能得到一个解决方案,该解决方案不涉及此处提到的内容,例如nginx,supervisor等  SOmeone,请helllppp meeeeee .......?

2 个答案:

答案 0 :(得分:3)

127.0.0.1表示容器内部与外部的不同。请改用0.0.0.0:5000

答案 1 :(得分:3)

进一步解释:Docker端口转发的工作方式是从主机上的IP转发到容器中的一个地址。具体来说,是与将容器连接到主机网络的Docker桥接网络对话的外部地址。它无法与容器上的127.0.0.1进行对话,因为这是一个专用IP,主机拥有自己的127.0.0.1

因此,您需要绑定到该Docker桥接网络,或者通过绑定到0.0.0.0来仅绑定到所有接口,这是最简单的解决方案。

用散文来解释这一点有些棘手,但是我的版本较长,其中还包括图表,因此可能更易于理解:https://pythonspeed.com/articles/docker-connection-refused/