Nodejs应用程序不是通过使用docker撰写的

我dockerizing nodejs和mongoDB应用程序,但它不执行到浏览器。

url是http://0.0.0.0:3030/

浏览器错误:

This site can't be reached The connection was reset. Try: Checking the connection Checking the proxy and the firewall ERR_CONNECTION_RESET 

通过执行这个命令:docker-compose up –build

它会产生下面的输出到ubuntuterminal。

 web_1 | npm info lifecycle test@1.0.0~prestart: test@1.0.0 web_1 | npm info lifecycle test@1.0.0~start: test@1.0.0 web_1 | web_1 | > test@1.0.0 start /usr/src/app web_1 | > node app.js web_1 | web_1 | App listning on port 3030 mongoDB_1 | 2017-05-16T07:07:27.891+0000 I NETWORK [initandlisten] connection accepted from 172.19.0.3:57655 #3 (1 connection now open) 

Dockerfile

 FROM node:alpine RUN mkdir -p /usr/src/app WORKDIR /usr/src/app RUN npm install nodemon -g COPY package.json /usr/src/app/ RUN npm install COPY . /usr/src/app EXPOSE 3030 CMD [ "npm", "start" ] 

泊坞窗,compose.yml

 version: "2.0" services: web: build: . command: npm start ports: - "3030:3000" volumes: - .:/api links: - mongoDB mongoDB: image: mongo:3.0 ports: - "27017:27017" volumes: - /srv/docker/mongodb:/var/lib/mongodb restart: always 

的package.json

 { "name": "test", "version": "1.0.0", "description": "My first node docker application", "main": "index.js", "dependencies": { "express": "^4.15.2", "mongodb": "^2.2.26" }, "devDependencies": {}, "scripts": { "start": "node app.js" }, "author": "Muzammil", "license": "ISC" } 

Nodejs app.js

 'use strict'; const express = require('express'); const app = express(); const MongoClient = require('mongodb').MongoClient; const port = 3030; let DB; MongoClient.connect("mongodb://mongoDB/testDB", (err, db) => { console.log(err); //console.log(db); DB = db; }); app.get('/', (req, res)=>{ DB.collection("user").find({}, (err, result) => { if(err) { return res.json({message: "Error", error: err}); } let results = []; result.each((err, doc) => { if(doc) { console.log(doc); }else { res.end(); } }) //res.status(200).json({message: "Successfully", code: 200, data: result}); }); }); app.listen(port, ()=>{ console.log(`App listning on port ${port}`); }); 

你的docker-compose文件中有“3030:3000”,这意味着你要将localhost的端口3030绑定到容器的端口3000.所以,要么暴露端口3000,要么将行改为“3030:3030”

Interesting Posts