Docker:无法打开容器到主机的端口 [英] Docker: cannot open port from container to host

查看:127
本文介绍了Docker:无法打开容器到主机的端口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Docker for Mac 。我有一个运行服务器的容器,例如我的服务器在端口5000上运行。我在 Dockerfile

I'm using Docker for Mac. I have a container that run a server, for example my server is run on port 5000. I have exposed this port on Dockerfile

当我的容器正在运行时,我连接到该容器并通过运行以下命令来检查该服务器是否正常工作,并查看它是否返回数据(一堆html和javascript)

When my container is running, I connect to this container and check and if this server is working or not by running below command and see that it returns data (a bunch of html and javascript)

wget -d localhost:5000

注意,我启动了这个容器,并通过命令在外部发布端口:

Notes, I start this container and also publish port outside by command:

docker run -d -p 5000:5000 <docker_image_name>

但是在Docker主机上(是我的Mac,运行El Capitan),我打开chrome并转到地址 localhost:5000 。没用请注意,如果我转到任意端口,例如 localhost:4000 ,我会看到来自chrome的错误消息,例如:

But at docker host (is my mac and running El Capitan), I open chrome and go to address localhost:5000. It doesn't work. Just a little note, if I go to any arbitrary port such as localhost:4000 I see error message from chrome such as:

This site can’t be reached
localhost refused to connect.

但是 localhost:5000 的错误消息是:

The localhost page isn’t working
localhost didn’t send any data.

所以看来我已经稍微配置了工作,但是出了点问题。

So it seems I have configured work "a little" but something wrong. Please tell me how to fix this.

推荐答案

请检查容器中的程序是否正在监听接口0.0.0.0。

Please check the program in container is listening on interface 0.0.0.0.

在容器中,运行命令:

ss -lntp

如果出现以下情况:

LISTEN  0   128   127.0.0.1:5000  *:*

这意味着您的Web应用程序仅监听在本地主机上,因此容器主机无法访问您的Web应用。您应该通过更改Web应用程序的构建设置,使服务器在 0.0.0.0 界面上进行监听。

that means your web app only listen at localhost so container host cannot access to your web app. You should make your server listen at 0.0.0.0 interface by changing your web app build setting.

例如,如果您的服务器是nodejs应用程序:

For example if your server is nodejs app:

var app = connect().use(connect.static('public')).listen(5000, "0.0.0.0");

如果您的服务器是Web Pack:

If your server is web pack:

 "scripts": {
    "dev": "webpack-dev-server --host 0.0.0.0 --port 5000 --progress"
  }

这篇关于Docker:无法打开容器到主机的端口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆