Docker not expose ports for node and webpack dev-server

醉酒当歌 提交于 2019-12-06 22:19:25

I think this issue might be related to the fact that you are specifying 127.0.0.1 as host. Try to use 0.0.0.0 instead:

var http = require('http');

http.createServer(function (req, res) {
    res.writeHead(200, {'Content-Type': 'text/plain'});
    res.end('Hello World\n'+new Date);
}).listen(3002, '0.0.0.0');

For subdomains you will most likely have to edit the /etc/hosts file on your local machine.

Rob Helps me to find main part solution of problem. I make tests and investigation and find out that when we set .listen(3002, '0.0.0.0') and edit /etc/hosts by add line like

127.0.0.1 node-test.local

And when we type in browser node-test.local:3002 it will works. However if we use webpack dev starter, in browser we get error:

Invalid Host header

So we need reconfugre webpack.js file (e.g. in angular-starter framework (ASF) it is in ./config/webpack.dev.js) by adding disableHostCheck: true - here is egzample of configuration:

devServer: {
  port: "3002",            // in ASF is METADATA.port,
  host: "0.0.0.0",         // in ASF is METADATA.host,     
  public: "0.0.0.0:3002",  // in ASF is METADATA.public,
  disableHostCheck: true,
  //...
}

After that http://node-test.local:3002 should works on MacOs Browsers.

If we need to have many subdomains on one port number (which is out of frame of this question) then probably we must use reverse-proxy (which is probably standard solution for docker multi-webapps(subdomains) over port 80)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!