2017-10-19 58 views
0

雖然試圖dockerize節點的應用程序,當我訪問本地主機:8000我得到這個錯誤:當我使用run命令圖像Dockerizing的Node.js應用

The connection was reset - the connection to the server was reset while the page was loading.

在終端,我得到所需的輸出在控制檯中。它說:

Server running at 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 

所有這些文件保存在同一個目錄中。

+0

你如何運行容器? – Sergiu

+0

sudo docker run -p 8000:8000 -it --name node-container2 my-node-image – Himansingh

+0

通過編輯將其添加到問題中。 – Veve

回答

1

只要改變你的index.js0.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/'); 

而且你將可以通過本地主機訪問主機上您的應用程序。

+0

Thanx很多Nickolay ..這工作:) – Himansingh

+0

@Himansingh你應該接受這個答案然後;) – Veve

+0

我已經接受但投票少於15聲譽不公開顯示公共。對不起:(謝謝你的幫助。 – Himansingh