Docker not exposing ports

时间:2018-03-25 21:06:10

标签: docker

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?

1 个答案:

答案 0 :(得分:2)

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.