Node.js 未使用docker compose运行Nodejs应用程序
我正在对接nodejs和mongoDB应用程序,但它并没有在浏览器中执行 URL是 浏览器中出现错误: 通过执行此命令: docker组装--构建 它将生成以下输出到ubuntu终端Node.js 未使用docker compose运行Nodejs应用程序,node.js,mongodb,docker,meanjs,Node.js,Mongodb,Docker,Meanjs,我正在对接nodejs和mongoDB应用程序,但它并没有在浏览器中执行 URL是 浏览器中出现错误: 通过执行此命令: docker组装--构建 它将生成以下输出到ubuntu终端 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 | > t
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
docker-compose.yml
package.json
Nodejs app.js
您的docker compose文件中有“-3030:3000”,这意味着您正试图将localhost端口3030绑定到容器端口3000。因此,要么公开端口3000,要么将行更改为“3030:3030”我也尝试过,但同样的错误我猜mongodb还没有准备好接受连接,应用程序因此崩溃。docker记录了什么输出?如果使用ok npm info结束,npm info有效npm@4.2.0npm信息使用node@v7.10.0npm信息生命周期test@1.0.0~prestart:test@1.0.0npm信息生命周期test@1.0.0~start:test@1.0.0 > test@1.0.0端口3030Ah上的start/usr/src/app>node app.js app列表找到了它。docker compose文件中有
-“3030:3000”
,这意味着您正试图将本地主机端口3030绑定到容器端口3000。因此,要么公开端口3000,要么将行更改为-“3030:3030”
谢谢@Creynders+1。请让我知道一件事,在执行docker compose up--build命令后,如何看到nodejs控制台?
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)
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" ]
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
{
"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"
}
'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}`);
});