Home > Software design >  Multiple Mongo Container
Multiple Mongo Container

Time:08-20

I wrote the following docker-compose file:

version: '3.7'
services:
  mongo-db-showcase-db:
    image: mongo:latest
    restart: always
    environment:
      MONGO_INITDB_ROOT_USERNAME: '${DB_USER}'
      MONGO_INITDB_ROOT_PASSWORD: '${DB_PASSWORD}'
    ports:
      - '27018:27017'

  frontend-showcase:
    build:
      context: showcase
      dockerfile: Dockerfile-frontend-showcase
    image: showcase-frontend-showcase-image:latest
    ports:
      - '80:80'

  backend-showcase:
    build:
      context: showcase
      dockerfile: Dockerfile-backend-showcase
    image: showcase-backend-showcase-image:latest
    environment:
      DATABASE_URL: 'mongodb://${DB_USER}:${DB_PASSWORD}@mongo-db-showcase-db:27018/'
    ports:
      - '3000:3000'
    links:
      - mongo-db-showcase-db

  mongo-db-admin-manager-db:
    image: mongo:latest
    restart: always
    environment:
      MONGO_INITDB_ROOT_USERNAME: '${DB_USER}'
      MONGO_INITDB_ROOT_PASSWORD: '${DB_PASSWORD}'
    ports:
      - '27017:27017'  

  frontend-manager:
    build:
      context: manager-app
      dockerfile: Dockerfile-frontend-manager
    image: manager-frontend-manager-image:latest
    ports:
      - '8080:80'
      
  backend-manager:
    build:
      context: manager-app
      dockerfile: Dockerfile-backend-manager
    image: manager-backend-manager-image:latest
    environment:
      DATABASE_URL: 'mongodb://${DB_USER}:${DB_PASSWORD}@mongo-db-admin-manager-db:27017/'
    ports:
      - '8000:8000'
    links:
      - mongo-db-admin-manager-db

Here I have two mongo db container, one reachable to port 27017 the other should be reachable to port 27018. The service connecting to the db into port 27017 connect fine, the other doesn't connect. The error I get is:

MongooseServerSelectionError: connect ECONNREFUSED 172.20.0.6:27018
    at NativeConnection.Connection.openUri (/app/node_modules/mongoose/lib/connection.js:807:32)
    at /app/node_modules/mongoose/lib/index.js:342:10
    at /app/node_modules/mongoose/lib/helpers/promiseOrCallback.js:32:5
    at new Promise (<anonymous>)
    at promiseOrCallback (/app/node_modules/mongoose/lib/helpers/promiseOrCallback.js:31:10)
    at Mongoose._promiseOrCallback (/app/node_modules/mongoose/lib/index.js:1176:10)
    at Mongoose.connect (/app/node_modules/mongoose/lib/index.js:341:20)
    at Object.<anonymous> (/app/backend/app.js:26:10)
    at Module._compile (internal/modules/cjs/loader.js:999:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10) {
  reason: TopologyDescription {
    type: 'Unknown',
    servers: Map { 'mongo-db-showcase-db:27018' => [ServerDescription] },
    stale: false,
    compatible: true,
    heartbeatFrequencyMS: 10000,
    localThresholdMS: 15,
    logicalSessionTimeoutMinutes: undefined
  }
}

172.20.0.6 is the address of the container. What could be the problem? Any suggestion is really appreciated. Thanks in advance to anyone that would want to answer.

CodePudding user response:

Your connection string port should be 27017 because all your container running inside a internal network, 27018 is the port you forwarded and can only connect from outside.

DATABASE_URL: 'mongodb://${DB_USER}:${DB_PASSWORD}@mongo-db-showcase-db:27017/'
  • Related