Docker Community Forums

Share and learn in the Docker community.

Linking a simple nodejs app with the mongodb container built from the crawlab docker image

Am trying to make a simple nodesjs app that shows the entirety of a collection from an existing mongodb database using docker, I have created the nodejs app and dockerized it, I also created a network that includes the containers of both the nodesjs app and the existing mongodb data base. the connection was established successfully but for some reason I don’t get any results back. you will find bellow the code I used to build my simple app :

The database am trying to connect to is called crawlab_test and the collection am trying to show on the nodejs app is called resultnews.

( When i access http://localhost:5001/ i get a result saying Cannot GET / but when i try to access http://localhost:5001/resultnews to show the collection using my nodejs app i get nothing. )

my server.js file :

const Resultnews = require("./src/Resultnews.model");
const express = require("express");
const app = express();
const connectDb = require("./src/connection");
const PORT = 5001;
app.get("/resultnews", async (req, res) => {
  const resultnews = await resultnews.find();
  res.json(resultnews);
});
app.listen(PORT, function () {
  console.log(`Listening on ${PORT}`);
  connectDb().then(() => {
    console.log("MongoDb connected");
  });
});

i created a folder called src inside it i created two files :

my Resultnews.model.js :

const mongoose = require("mongoose");
const resultnewsSchema = new mongoose.Schema({
  title: String,
  url: String,
  clicks: String,
  task_id: String,
});

const Resultnews = mongoose.model("Resultnews", resultnewsSchema);
module.exports = Resultnews;

And my connection.js file :

// connection.js
const mongoose = require("mongoose");
const User = require("./Resultnews.model");
const connection = "mongodb://localhost:27017/crawlab_test";
const connectDb = () => {
  return mongoose.connect(connection, {
    useNewUrlParser: true,
    useUnifiedTopology: true,
  });
};
module.exports = connectDb;

Here the Dockerfile i used :

FROM node:12
# Create app directory
WORKDIR /usr/src/app
# Install app dependencies
COPY package*.json ./
RUN npm install
# Copy app source code
COPY . .
#Expose port and start application
EXPOSE 5001
CMD [ "npm", "start" ]

the docker commands i used are on this project are :

docker network create enews to create the network,

docker network connect crawlab_mongo_1 to add the existing mongodb container (was created using crawlab docker image ) to my network ,

docker build -f Dockerfile -t mydockerapp . to build a container from my nodejs app and finally

docker run -d --net=enews --name mydockerapp -p 5001:5001 mydockerapp to run the container and add to my network.

ps : the app works fine when using a local mongodb database so am guessing the problem is from my docker run commands but i cant seem to find it. thank you in advance for everyone who might help me with this problem.