Tuesday, 16 May 2017

Nodejs application not running by using docker compose

I'm dockerizing nodejs and mongoDB application but its not executing into browser.

URL is http://0.0.0.0:3030/

Error in browser:

This site can’t be reached

The connection was reset.
Try:
Checking the connection
Checking the proxy and the firewall
ERR_CONNECTION_RESET

By executing this command: docker-compose up --build

It will generates following output into ubuntu terminal.

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

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

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}`);
});



via Muzammil Ahmed

No comments:

Post a Comment