Docker Community Forums

Share and learn in the Docker community.

Docker-compose up not working with Nodejs MongoDB API Restful

docker

(Mauriciohike) #1

Hello friends, how are you?

I’m a beginner with docker and I’m trying to put my restful api (made in nodejs with mongodb) into containers to run.

The problem is that I’m having an error when I try to start (with docker-compose up) I simply get a very strange error in cmd.

I’ll put the image and the api below

Thanks so much if you can help

./app/app.js:

var express = require('express');
var mongo = require('mongoose');
var bodyParser = require('body-parser');
var app = express();

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
    extended: true
}));

var mongoaddr = ('mongodb://' + process.env.MONGO_PORT_27017_TCP_ADDR + ' 
:27017/testeapi');
console.log(mongoaddr);
mongo.connect(mongoaddr);

var taskListSchema = mongo.Schema({
    descricao : { type: String },
    concluido : Boolean,
    updated_at: { type: Date, default: Date.now },
});

var Model = mongo.model('Tasks', taskListSchema);
    app.post("/api/add", function (req, res) {
    var register = new Model({
	    'descricao' : req.body.descricao,
	    'concluido' : req.body.concluido
    });
    register.save(function (err) {
	    if (err) {
		    console.log(err);
		    res.send(err);
		res.end();
	     }
    });
    res.send(register);
    res.end();
});

app.get("/api/all", function (req, res) {
    Model.find(function(err, todos) {
	    if (err) {
		    res.json(err);
	    } else {
		    res.json(todos);
	    }
    })
});

app.delete("/api/delete/:id", function (req, res) {
 Model.findByIdAndRemove(req.params.id, req.body, function (err, post) {
    if (err) return next(err);
    res.json(post);
  });
});

app.listen(8080, function() {
    console.log('Funcionando');
});

./app/package.json:

{
  "name": "node-app",
  "version": "1.0.0",
  "description": "",
  "main": "app.js",
  "scripts": {
    "start": "nodemon app.js"
  },
  "author": "",
  "license": "MIT",
  "dependencies": {
    "express": "^4.14.0",
    "mongoose": "~4.6.1",
    "nodemon": "^1.10.2",
    "body-parser": "1.15.2"
  }
}

Dockerfile:

FROM node:7.1
RUN mkdir -p /usr/src/app
RUN npm install nodemon -g
WORKDIR /usr/src/app
COPY app/package.json /usr/src/app/package.json
RUN npm install -g
EXPOSE 8080
EXPOSE 5858
CMD [“npm”, “start”]

docker-compose.yml:

version: '3'
networks:
  app_net:
    external:
      name: app_net
services:
  mongo:
    image: mongo:latest
    ports:
      - "27017:27017"
    volumes:
      - './data:/data/db'
    networks: 
      - app_net
  application:
    build: .
    command: bash -c "npm install; nodemon app.js --color=always"
    ports:
      - "8080:8080"
      - "5858:5858"
    volumes:
      - ./app:/usr/src/app
    links:
      - mongo
    networks: 
      - app_net