简体   繁体   English

docker 容器上的服务器访问问题

[英]Problem acessing server on docker container

I am trying to build and run docker image from example on this site: https://kubernetes.io/docs/tutorials/hello-minikube/我正在尝试从本网站的示例中构建和运行 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

I use commands我使用命令

docker build -t nsj .
docker run nsj

They run without error but I cannot access the server on localhost:8080.它们运行没有错误,但我无法访问 localhost:8080 上的服务器。

What is wrong?怎么了?

Seems like at least two things are wrong:似乎至少有两件事是错误的:

  1. You need to map the port from your docker host您需要 map docker 主机的端口
  2. You need to bind your server to 0.0.0.0您需要将服务器绑定到 0.0.0.0

So, probably these changes (untested):所以,可能是这些变化(未经测试):

In your code:在您的代码中:

www.listen(8080, "0.0.0.0");

In your docker command:在您的 docker 命令中:

docker run nsj -p 8080:8080

Note that having EXPOSE 8080 in your Dockerfile does not actually expose anything.请注意,在Dockerfile中使用EXPOSE 8080实际上不会暴露任何东西。 It just "marks" this port in the docker engine's metadata and is intended for both documentation (so people reading the Dockerfile know what it does) and for tools that inspect the docker engine.它只是在 docker 引擎的元数据中“标记”此端口,并且适用于两个文档(因此阅读Dockerfile的人知道它的作用)和检查 Z05B6053C41A2130AFD4E4E66FC3B158BDA 引擎的工具。

To quote from the reference :从参考文献中引用

The EXPOSE instruction does not actually publish the port. EXPOSE 指令实际上并不发布端口。 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它充当构建映像的人和运行容器的人之间的一种文档类型,关于打算发布哪些端口

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM