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

Nodejs application not running by using docker compose

我正在 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-组成--build

它将生成以下输出到 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      | > 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)

Docker 文件

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" ]

docker-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"
}

Node.js 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”,这意味着你正在尝试将本地主机端口 3030 绑定到容器端口 3000。因此,要么公开端口 3000,要么更改该行 - “3030:3030”