问题
I have MacOS High Sierra and my goal is to run node web-applications without installation node on macos (I wanna use docker to do it). That web-application is usually angular-webpack (compilation + run dev serwer).
Dockerfile:
FROM node
WORKDIR /work
CMD while true; do sleep 10000; done
EXPOSE 3002
The line CMD while ...
makes that container will be not killed by docker after run it - this allow us to "login" into container (by docker exec -it...
).
Bash script run.cmd which run container:
set -e
docker build -t node-cmd .
docker rm -f node-cmd |:
docker run -d --name node-cmd -p 3002:3002 -v /Volumes/work:/work node-cmd
docker exec -it node-cmd /bin/bash
Where /Volumes/work
is directory on my MacOs which contains angular projects.
Here is example application in file /work/tmp/example.js (mapped in container) for which problem is visible (however them main goal is to find proper configuration of webpack dev-server):
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, '127.0.0.1');
console.log('Server running at http://127.0.0.1:3002/');
So first I run run.cmd
- here is what docker ps
write:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e22188d8e136 node-cmd "/bin/sh -c 'echo \"R…" 16 seconds ago Up 15 seconds 0.0.0.0:3002->3002/tcp node-cmd
Inside container I type commands:
root@5cd3773e7815:/work# cd tmp
root@5cd3773e7815:/work# node example.js &
[1] 18
root@5cd3773e7815:/work/tmp# Server running at http://127.0.0.1:3002/
So serwer was start. Now I test:
root@5cd3773e7815:/work/tmp# curl 127.0.0.1:3002
Hello World
So inside docker everything works fine. Now when I go to my macos Chrome browser http://127.0.0.1:3002
i get
This page isn’t working 127.0.0.1 didn’t send any data.
ERR_EMPTY_RESPONSE
If I remove -p 3002:3002
from run.cmd file the browser response change to
127.0.0.1 refused to connect. Try: Checking the connection Checking the proxy and the firewall
ERR_CONNECTION_REFUSED
I also try tu use docker run --ip=127.0.0.1...
but I get message that
docker: Error response from daemon: user specified IP address is supported on user defined networks only.
When i try to use docker run --net=host ...
it doesn't change anything
I alswo wanna mention that when I run mysql
image by docker run -p 3306:3306 ...
I works properly (!) - so may be the problem is with node
image (?)
Questions:
- How to "unblock" port 3002 in container to allow host browser to connect with service inside container?
- How to use subdomains like
subdomain.local:3002
too? - How to use subdomains using webpack devServer (used in angular) instead above
example.js
(i need to have run many angular web-application each on different port with individual subdomain) ?
回答1:
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.
回答2:
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)
来源:https://stackoverflow.com/questions/51749513/docker-not-expose-ports-for-node-and-webpack-dev-server