Docker不公开端口 [英] Docker not exposing ports

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

问题描述

我正在尝试将容器的端口3000暴露给主机。这是我正在使用的命令:

I am trying to expose port 3000 of my container to the host. This is the command I am using:

docker run -ti --expose=3000 -p 3000:3000 dockerName

PORTS 部分docker ps 我看到 0.0.0.0:3000->3000/tcp

On the PORTS section of docker ps I see 0.0.0.0:3000->3000/tcp.

当我尝试在该端口上连接到简单的NodeJS服务器时,会显示无法连接。我正在运行Docker 18.03.0-ce和最新版本的NodeJS。

When I try to connect to a simple NodeJS server on that port I get Unable to connect. I am running Docker 18.03.0-ce and the latest version of NodeJS.

我尝试通过本地主机IP(172.17)本地主机(127.0.0.1)连接.0.1),由主机OS发出的IP(172.17.0.1)和主机IP均未成功。我还完全禁用了主机操作系统上的防火墙。

I try to connect through localhost (127.0.0.1) IP, the internal container IP (172.17.0.1), the IP issued by the host OS (172.17.0.1) and the host IP all without success. I have also completely disabled the firewall on the host OS.

这是NodeJS代码,在主机上可以正常工作:

Here is the NodeJS code, which works fine on the host:

var http = require('http');
http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello World\n');
}).listen(3000, "127.0.0.1");
console.log('Server running at http://127.0.0.1:3000/');

有什么办法可以使它在容器中工作并通过主机OS连接到它?

Is there any way to get this to work inside the container and connect to it through the host OS?

推荐答案

无法连接的原因是因为Node.js应用程序监听IP 127.0.0.1 在容器外部不可用。

The reason you cannot connect is because your Node.js app listens on IP 127.0.0.1 which is not available outside the container.

更改您的应用程序,使其监听IP 0.0.0.0 ,您将可以正确连接。

Change your app so that it listens on IP 0.0.0.0 and you will be able to connect properly.

这篇关于Docker不公开端口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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