Dockerizing的Node.js应用

问题描述:

虽然试图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

只要改变你的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