我正在尝试从该站点上的示例构建和运行 docker 镜像:https://kubernetes.io/docs/tutorials/hello-minikube/
//server.js
var http = require('http');
var handleRequest = function(request, response) {
console.log('Received request for URL: ' + request.url);
response.writeHead(200);
response.end('Hello World!');
};
var www = http.createServer(handleRequest);
www.listen(8080);
//Dockerfile
FROM node:6.14.2
EXPOSE 8080
COPY server.js .
CMD node server.js
我使用命令
docker build -t nsj .
docker run nsj
它们运行没有错误,但我无法访问 localhost:8080 上的服务器。
怎么了?
最佳答案
似乎至少有两件事是错误的:
所以,可能是这些变化(未经测试):
在您的代码中:
www.listen(8080, "0.0.0.0");
在您的 docker 命令中:
docker run nsj -p 8080:8080
请注意,有
EXPOSE 8080
在您的 Dockerfile
实际上并没有暴露任何东西。它只是在 docker 引擎的元数据中“标记”这个端口,并且用于文档(因此阅读 Dockerfile
的人知道它的作用)和检查 docker 引擎的工具。引用 reference :
The EXPOSE instruction does not actually publish the port. It functions as a type of documentation between the person who builds the image and the person who runs the container, about which ports are intended to be published
关于docker - 在 docker 容器上访问服务器时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58560990/