Cannot connect node js app to mongodb running in a docker swarm
NickName:Mosa Sello Ask DateTime:2021-05-04T16:08:03

Cannot connect node js app to mongodb running in a docker swarm

I am trying to deploy nodejs app in docker swarm. However I'm struggling to make it connect to the mongodb service. Below is my docker-compose file

version: '3'
services:
  moitrack-teltonika-parser:
    build: .
    image: moitrack/moitrack-teltonika-parser:1.0.14
    ports:
      - "5001:5001"
    networks:
      - web
    deploy:
      mode: replicated
      replicas: 2
      update_config:
        parallelism: 2
        delay: 10s
      restart_policy:
        condition: on-failure
        delay: 5s
        max_attempts: 3
        window: 120s
    depends_on: 
      - db
  db:
    image: mongo
    command: mongod --bind_ip_all
    ports:
    - "27017"
    volumes:
    - mongodata:/data/db
    restart: "always"
    deploy:
      placement:
        constraints: [node.role == manager]
    networks:
    - web
volumes:
  mongodata: {}
      
networks:
  web:
    external: true

This is the node js script

var express = require("express");
const mongoose = require('mongoose');
var server = express();

const options = {
    useNewUrlParser: true,
    reconnectTries: Number.MAX_VALUE,
    reconnectInterval: 500,
    connectTimeoutMS: 10000,
  };

const DB_URI = 'mongodb://db:27017/moiponeGateKeeper';

mongoose.connect(DB_URI, options).then(() => {
    console.log("database connected");

}).catch(err => console.log(err));

server.listen(5001, () => { console.log("Server started"); });

I get the error below when I view the node service in the docker swarm. enter image description here

Copyright Notice:Content Author:「Mosa Sello」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/67380974/cannot-connect-node-js-app-to-mongodb-running-in-a-docker-swarm

More about “Cannot connect node js app to mongodb running in a docker swarm” related questions

Cannot connect node js app to mongodb running in a docker swarm

I am trying to deploy nodejs app in docker swarm. However I'm struggling to make it connect to the mongodb service. Below is my docker-compose file version: '3' services: moitrack-teltonika-parse...

Show Detail

Connect nodejs App in one docker container to mongodb in another on Docker Swarm

I have setup a docker stack (see docker-compose.yml) below. It has 2 Services: - A nodejs (loopback) service running in one (or more) containers. - A mongodb running in another container. What HO...

Show Detail

Cannot connect to MongoDB via node.js in Docker

My node.js express app cannot connect to the MongoDB in a Docker. I'm not that familiar with Docker. node.js connection: import mongodb from 'mongodb'; ... mongodb.MongoClient.connect('mongodb://

Show Detail

Not able to connect by using network on docker to mongodb running in docker container other container with node js

I have created a network within a docker with name favorites-net and two containers with the name following :- 1). mongodb (its running properly image from docker hub mongodb). 2). favorites (with ...

Show Detail

Nginx configuration for connecting to external mongodb from docker swarm

I have a docker swarm running 2 python flask services, each one running in a different region, say A, and B. Region A is also running Nginx, which is the main entrypoint to access the services. The...

Show Detail

Zerotier running in a Docker Swarm Service

I have a docker swarm with 3 nodes. Each node has a mongodb service running. I want to be able to connect to the database using Robo 3T from my local device, but I don't want to expose any ports. Is

Show Detail

Cannot connect Node to mongoDB in docker container on first try

I'm trying to dockerize Node.js application which connects to MongoDB using mongoose. It succeeds anytime I run node index.js from the shell when the connection URL is: mongodb://localhost:27017/de...

Show Detail

Docker-swarm >> Cannot connect to the docker engine endpoint

docker version 1.9.1 swarm version 1.0.1 why on connecting 3 VMs (bridged net) to swarm. "docker info" shows me all nodes Status pending. 1 of 3 hosts is manager all output is from t...

Show Detail

Access to service list on docker swarm worker node

I am using docker-API module for accessing service list and the replica information. It works better in swarm-mode and on manager node. But if the app goes on worker node, it gives me the following...

Show Detail

Docker Swarm Linking

I want to create a Docker Swarm Cluster running an elastic search instance, a MongoDB instance and a grails app, each on a separate machine. I'm using Docker Machine to set up my Docker Swarm Clust...

Show Detail