server.listen(port, '127.0.0.1') 无法访问容器化节点服务器
Containerized Node server inaccessible with server.listen(port, '127.0.0.1')
我在 Docker 中设置了一个简单的节点服务器。
Docker文件
FROM node:latest
RUN apt-get -y update
ADD example.js .
EXPOSE 1337
CMD node example.js
example.js
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n'+new Date);
}).listen(1337, '127.0.0.1');
console.log('Server running at http://127.0.0.1:1337/');
现在构建镜像
$ docker build -t node_server .
现在 运行 在容器中
$ docker run -p 1337:1337 -d node_server
$ 5909e87302ab7520884060437e19ef543ffafc568419c04630abffe6ff731f70
验证容器是 运行ning 并且端口已映射:
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5909e87302ab node_server "/bin/sh -c 'node exa" 7 seconds ago Up 6 seconds 0.0.0.0:1337->1337/tcp grave_goldberg
现在让我们附加到容器并验证服务器 运行ning 在里面:
$ docker exec -it 5909e87302ab7520884060437e19ef543ffafc568419c04630abffe6ff731f70 /bin/bash
并在容器命令行中输入:
root@5909e87302ab:/# curl http://localhost:1337
Hello World
Mon Feb 15 2016 16:28:38 GMT+0000 (UTC)
看起来不错吧?
问题
当我在主机上执行相同的 curl 命令(或使用浏览器导航到 http://localhost:1337)时,我什么也没看到。
知道为什么容器和主机之间的端口映射不起作用吗?
我已经尝试过的事情:
- 运行
--expose 1337
标志
Adding EXPOSE 1337 to the docker file
EXPOSE
是 强制性的 如果你想 "expose" 该端口到其他容器。
作为 BMitch 评论:
Expose
isn't needed to publish a port or to connect container to container over a shared docker network.
It's metadata for publishing all ports with -P
and inspecting the image/container.
所以:
Running with the --expose 1337
flag
不完全是:您需要 docker run it with -p 1337:1337
您需要:
- 构建一个包含
EXPOSE
指令的镜像(由 -P
使用)
- 或 运行 它与主机上发布的端口
-p 1337:1337
测试 curl http://localhost:1337
是在容器内完成的(不需要 EXPOSE
或发布)。
如果你想让它在 Linux 主机上工作,你需要 EXPOSE+-P
或者 你需要 -p 1337:1337
.
要么。
单独声明它 expose 有助于记录意图,但不能单独做任何事情。
例如:
在该图中,8080 已公开,发布到 Linux 主机 8888。
如果 Linux 主机不是实际主机,则需要将同一端口快速转发到实际主机。参见“How to access tomcat running in docker container from browser?”。
如果 localhost 在 Linux 主机上不起作用,请尝试其 IP 地址:
CID=$(docker run -p 1337:1337 -d node_server)
CIP=$(docker inspect --format '{{ .NetworkSettings.IPAddress }}' ${CID})
curl http://${CIP}:1337
或者,如上所述,让您的服务器监听来自任何 IP 的连接:0.0.0.0
即 the broadcast address or zero network.
您的端口已正确公开,但您的服务器正在侦听容器内 127.0.0.1
上的连接:
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n'+new Date);
}).listen(1337, '127.0.0.1');
您需要像这样运行您的服务器:
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n'+new Date);
}).listen(1337, '0.0.0.0');
注意 0.0.0.0 而不是 127.0.0.1。
我在 Docker 中设置了一个简单的节点服务器。
Docker文件
FROM node:latest
RUN apt-get -y update
ADD example.js .
EXPOSE 1337
CMD node example.js
example.js
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n'+new Date);
}).listen(1337, '127.0.0.1');
console.log('Server running at http://127.0.0.1:1337/');
现在构建镜像
$ docker build -t node_server .
现在 运行 在容器中
$ docker run -p 1337:1337 -d node_server
$ 5909e87302ab7520884060437e19ef543ffafc568419c04630abffe6ff731f70
验证容器是 运行ning 并且端口已映射:
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5909e87302ab node_server "/bin/sh -c 'node exa" 7 seconds ago Up 6 seconds 0.0.0.0:1337->1337/tcp grave_goldberg
现在让我们附加到容器并验证服务器 运行ning 在里面:
$ docker exec -it 5909e87302ab7520884060437e19ef543ffafc568419c04630abffe6ff731f70 /bin/bash
并在容器命令行中输入:
root@5909e87302ab:/# curl http://localhost:1337
Hello World
Mon Feb 15 2016 16:28:38 GMT+0000 (UTC)
看起来不错吧?
问题
当我在主机上执行相同的 curl 命令(或使用浏览器导航到 http://localhost:1337)时,我什么也没看到。
知道为什么容器和主机之间的端口映射不起作用吗?
我已经尝试过的事情:
- 运行
--expose 1337
标志
Adding EXPOSE 1337 to the docker file
EXPOSE
是 强制性的 如果你想 "expose" 该端口到其他容器。
作为 BMitch 评论:
Expose
isn't needed to publish a port or to connect container to container over a shared docker network.
It's metadata for publishing all ports with-P
and inspecting the image/container.
所以:
Running with the
--expose 1337
flag
不完全是:您需要 docker run it with -p 1337:1337
您需要:
- 构建一个包含
EXPOSE
指令的镜像(由-P
使用) - 或 运行 它与主机上发布的端口
-p 1337:1337
测试 curl http://localhost:1337
是在容器内完成的(不需要 EXPOSE
或发布)。
如果你想让它在 Linux 主机上工作,你需要 EXPOSE+-P
或者 你需要 -p 1337:1337
.
要么。
单独声明它 expose 有助于记录意图,但不能单独做任何事情。
例如:
在该图中,8080 已公开,发布到 Linux 主机 8888。
如果 Linux 主机不是实际主机,则需要将同一端口快速转发到实际主机。参见“How to access tomcat running in docker container from browser?”。
如果 localhost 在 Linux 主机上不起作用,请尝试其 IP 地址:
CID=$(docker run -p 1337:1337 -d node_server)
CIP=$(docker inspect --format '{{ .NetworkSettings.IPAddress }}' ${CID})
curl http://${CIP}:1337
或者,如上所述,让您的服务器监听来自任何 IP 的连接:0.0.0.0
即 the broadcast address or zero network.
您的端口已正确公开,但您的服务器正在侦听容器内 127.0.0.1
上的连接:
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n'+new Date);
}).listen(1337, '127.0.0.1');
您需要像这样运行您的服务器:
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n'+new Date);
}).listen(1337, '0.0.0.0');
注意 0.0.0.0 而不是 127.0.0.1。