Docker Build无法创建server.js



我正在为Docker编写教程,我正在学习Docker构建。在本教程中,这是docker文件

FROM ubuntu:14.04
RUN apt-get update -y
RUN apt-get install -y curl
RUN curl -sL https://deb.nodesource.com/setup_4.x | bash -
RUN apt-get install -y node.js
COPY server.js /
EXPOSE 8080
CMD [ "node", "/server.js"]

在与dockerfile相同的目录中有一个server.js文件:

// Load the http module to create an http server.
var http = require('http');
// Configure our HTTP server to respond with Hello World to all requests
var server = http.createServer(function (request, response) {
response.writeHead(200, {"Content-Type": "Text/plain"});
response.end("Hello world!");
})
// Listen on port 8080
server.listen(8080, function() {
console.log('Server listening...');
})

在命令行中,我运行docker构建:docker build -t ahawkins/docker-into-hello-world .

然后我从上面的图像运行容器:docker run -d -p 8080:8080 ahawkins/docker-intro-hello-world

我希望curl localhost:8080"Hello world!"响应,但我得到的却是:

curl: (52) Empty reply from server

我ssh到容器中,发现server.js不在哪里,而且我甚至找不到节点安装。

我是否正确安装了节点?我应该期望在操作系统的根目录中看到server.js文件吗?为什么我没有看到一个,";你好,世界&";,从我的卷曲命令?

NodeJS附带14.04图像可能会令人难以置信。我建议使用现成的节点映像来构建nodejs应用程序。试试这个手册-https://nodejs.org/fr/docs/guides/nodejs-docker-webapp/

我会在Dockerfile中尝试以下操作,构建并尝试再次运行。我替换了"/"带有"从COPY命令将其自由插入图像中。(将server.js放在构建Dockerfile的同一目录中。(

FROM ubuntu:14.04
RUN apt-get update -y
RUN apt-get install -y curl
RUN curl -sL https://deb.nodesource.com/setup_4.x | bash -
RUN apt-get install -y node.js
COPY server.js .
EXPOSE 8080
CMD [ "node", "server.js"]

您是否尝试打开浏览器并键入";localhost:8080";看到";Hello World"消息

我希望这会有所帮助。之前已经给出的答案,也有一个很好的链接,以顺利的文档。

我使用的参考资料/资源:Bret Fisher 的Udemy Class

最新更新