问题描述
我有一个应用程序,其唯一依赖项是烧瓶,它在 docker 外部运行良好并绑定到默认端口 5000
.这是完整的来源:
I have an app whose only dependency is flask, which runs fine outside docker and binds to the default port 5000
. Here is the full source:
问题是当我在 docker 中部署它时,服务器正在运行但无法从容器外部访问.
The problem is that when I deploy this in docker, the server is running but is unreachable from outside the container.
下面是我的 Dockerfile.该图像是安装了烧瓶的 ubuntu.tar 只包含上面列出的 index.py
;
Below is my Dockerfile. The image is ubuntu with flask installed. The tar just contains the index.py
listed above;
这是我正在执行的部署步骤
Here are the steps I am doing to deploy
<代码>$>sudo docker build -t perfektimprezy .
据我所知,上面运行良好,图像在 /srv
中有 tar 的内容.现在,让我们在容器中启动服务器:
As far as I know the above runs fine, the image has the contents of the tar in /srv
. Now, let's start the server in a container:
它真的在运行吗?
是的,似乎烧瓶服务器正在运行.这就是奇怪的地方.让我们向服务器发出请求:
Yep, seems like the flask server is running. Here is where it gets weird. Lets make a request to the server:
空回复...但是进程正在运行吗?
Empty reply... But is the process running?
现在让我们通过 ssh 进入服务器并检查...
Now let's ssh into the server and check...
没关系...但不是从外面.
我做错了什么?
It's fine... But not from the outside.
What am I doing wrong?
推荐答案
问题是你只是绑定到 localhost 接口,如果你想让容器绑定到 0.0.0.0
可从外部访问.如果你改变:
The problem is you are only binding to the localhost interface, you should be binding to 0.0.0.0
if you want the container to be accessible from outside. If you change:
到
它应该可以工作.
请注意,这将绑定到主机上的所有接口,这在某些情况下可能存在安全风险 - 请参阅 https://stackoverflow.com/a/58138250/4332 了解有关绑定到特定接口的更多信息.
Note that this will bind to all interfaces on the host, which may in some circumstances be a security risk - see https://stackoverflow.com/a/58138250/4332 for more information on binding to a specific interface.
这篇关于在 docker 中部署一个最小的烧瓶应用程序 - 服务器连接问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!