[英]Docker Build Fails To Create 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 build: 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
文件嗎? 為什么我沒有看到“Hello World”? 從我的 curl 命令?
帶有 14.04 圖像的 NodeJS 可能是偽造的。 我建議使用現成的節點映像來構建 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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.