Docker not exposing ports

2019-08-16 10:00发布

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

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

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.

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.

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/');

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

标签: docker
1条回答
Emotional °昔
2楼-- · 2019-08-16 10:21

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.

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

查看更多
登录 后发表回答