Dockerizing Node.js应用程序
当试图dockerize节点的应用程序,当我访问本地主机:8000我得到这个错误:
连接被重置 – 连接到服务器被重置,而页面加载。
在terminal上,当我在图像上使用运行命令时,我在控制台中得到所需的输出。 它说:
运行在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/');
你将能够通过主机上的本地主机访问你的应用程序。