[英]Dockerizing Node.js app
嘗試對節點應用程序進行dockerize時,當我訪問localhost:8000時,出現以下錯誤:
連接已重置-加載頁面時重置了與服務器的連接。
在終端上使用image上的run命令時,會在控制台中獲得所需的輸出。 它說:
服務器運行在http:// localhost:8000 /
Dockerfile:
FROM node
RUN mkdir -p /app/
WORKDIR /app
COPY package.json /app
RUN cd /app
RUN npm install
COPY . /app
CMD ["node", "index.js"]
EXPOSE 8000
index.js:
#!/usr/bin/env nodejs
var http = require('http');
http.createServer(function(req, res) {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('Hello World\n');
}).listen(8000, 'localhost');
console.log('Server running at http://localhost:8000/');
的package.json:
{
"name": "server1",
"version": "1.0.0",
"description": "Dockerizing node-app",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Himanshu",
"license": "ISC"
}
這是我使用的運行命令
sudo docker run -p 8000:8000 -it --name node-container2 my-node-image
所有這些文件都保存在同一目錄中。
只需將index.js
更改為可在容器內的0.0.0.0
:
#!/usr/bin/env nodejs
var http = require('http');
http.createServer(function(req, res) {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('Hello World\n');
}).listen(8000, '0.0.0.0');
console.log('Server running at http://0.0.0.0:8000/');
您將能夠通過主機上的localhost訪問您的應用程序。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.