skip to Main Content

I am not able to start the mongodb in local using the docker container, below were the error and configurations used

  • Tryig to start mongodb as a container in local for connecting through spring boot application
ould not connect to database using connectionString: mongodb://root:pass12345@mongodb:27017/"
mongo-express  | /node_modules/mongodb/lib/sdam/topology.js:292
mongo-express  |                 const timeoutError = new error_1.MongoServerSelectionError(`Server selection timed out after ${serverSelectionTimeoutMS} ms`, this.description);
mongo-express  |                                      ^
mongo-express  | 
mongo-express  | MongoServerSelectionError: getaddrinfo ENOTFOUND mongodb
mongo-express  |     at Timeout._onTimeout (/node_modules/mongodb/lib/sdam/topology.js:292:38)
mongo-express  |     at listOnTimeout (node:internal/timers:569:17)
mongo-express  |     at process.processTimers (node:internal/timers:512:7) {
mongo-express  |   reason: TopologyDescription {
mongo-express  |     type: 'Unknown',
mongo-express  |     servers: Map(1) {
mongo-express  |       'mongodb:27017' => ServerDescription {
mongo-express  |         address: 'mongodb:27017',
mongo-express  |         type: 'Unknown',
mongo-express  |         hosts: [],
mongo-express  |         passives: [],
mongo-express  |         arbiters: [],
mongo-express  |         tags: {},
mongo-express  |         minWireVersion: 0,
mongo-express  |         maxWireVersion: 0,
mongo-express  |         roundTripTime: -1,
mongo-express  |         lastUpdateTime: 1701047,
mongo-express  |         lastWriteDate: 0,
mongo-express  |         error: MongoNetworkError: getaddrinfo ENOTFOUND mongodb
mongo-express  |             at connectionFailureError (/node_modules/mongodb/lib/cmap/connect.js:387:20)
mongo-express  |             at Socket.<anonymous> (/node_modules/mongodb/lib/cmap/connect.js:310:22)
mongo-express  |             at Object.onceWrapper (node:events:632:26)
mongo-express  |             at Socket.emit (node:events:517:28)
mongo-express  |             at emitErrorNT (node:internal/streams/destroy:151:8)
mongo-express  |             at emitErrorCloseNT (node:internal/streams/destroy:116:3)
mongo-express  |             at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {
mongo-express  |           cause: Error: getaddrinfo ENOTFOUND mongodb
mongo-express  |               at GetAddrInfoReqWrap.onlookup [as oncomplete] (node:dns:107:26) {
mongo-express  |             errno: -3008,
mongo-express  |             code: 'ENOTFOUND',
mongo-express  |             syscall: 'getaddrinfo',
mongo-express  |             hostname: 'mongodb'
mongo-express  |           },
mongo-express  |           [Symbol(errorLabels)]: Set(1) { 'ResetPool' }
mongo-express  |         },
mongo-express  |         topologyVersion: null,
mongo-express  |         setName: null,
mongo-express  |         setVersion: null,
mongo-express  |         electionId: null,
mongo-express  |         logicalSessionTimeoutMinutes: null,
mongo-express  |         primary: null,
mongo-express  |         me: null,
mongo-express  |         '$clusterTime': null
mongo-express  |       }
mongo-express  |     },
mongo-express  |     stale: false,
mongo-express  |     compatible: true,
mongo-express  |     heartbeatFrequencyMS: 10000,
mongo-express  |     localThresholdMS: 15,
mongo-express  |     setName: null,
mongo-express  |     maxElectionId: null,
mongo-express  |     maxSetVersion: null,
mongo-express  |     commonWireVersion: 0,
mongo-express  |     logicalSessionTimeoutMinutes: null
mongo-express  |   },
mongo-express  |   code: undefined,
mongo-express  |   [Symbol(errorLabels)]: Set(0) {}
mongo-express  | }
mongo-express  | 
mongo-express  | Node.js v18.19.0
mongo-express exited with code 1
version: "3.8"
services:
  mongodb:
    image: mongo
    container_name: mongodb
    environment:
      - MONGO_INITDB_ROOT_USERNAME=root
      - MONGO_INITDB_ROOT_PASSWORD=pass12345
    volumes:
      - /Users/Shared/Docker/mongodb-data:/data/db
    networks:
      - tgc-network
    ports:
      - 27017:27017
    healthcheck:
      test: echo 'db.runCommand("ping").ok' | mongo 10.10.10.60:27017/test --quiet
      interval: 30s
      timeout: 10s
      retries: 3
    restart: unless-stopped
  mongo-express:
    image: mongo-express
    container_name: mongo-express
    environment:
      - ME_CONFIG_MONGODB_SERVER=mongodb
      - ME_CONFIG_MONGODB_ENABLE_ADMIN=true
      - ME_CONFIG_MONGODB_ADMINUSERNAME=root
      - ME_CONFIG_MONGODB_ADMINPASSWORD=****
      - ME_CONFIG_BASICAUTH_USERNAME=admin
      - ME_CONFIG_BASICAUTH_PASSWORD=admin123
    depends_on:
      - mongodb
    networks:
      - tgc-network
    ports:
      - 8083:8081
    healthcheck:
      test: wget --quiet --tries=3 --spider http://admin:[email protected]:8081 || exit 1
      interval: 30s
      timeout: 10s
      retries: 3
    restart: unless-stopped
volumes:
  mongodb-data:
    name: mongodb-data
networks:
  tgc-network:
    name: tgc-network

Not able to find what’s going wrong in the configurations

  • I tried removing the images and restart, but it’s reccurring

2

Answers


  1. Seems that your environment is not set correctly

    • ME_CONFIG_MONGODB_ADMINPASSWORD=****
    Login or Signup to reply.
  2. This can be due to mongodb service not being ready when express is trying to connect.
    In your Docker Compose file, you have already specified that mongo-express depends on mongodb using depends_on. Unfortunately, depends_on waits for the container and not for the service inside the container.

    Try adding a wait script in your mongo-express service to ensure that it only starts after the mongodb service is ready like this:

    version: "3.8"
    services:
         ...
      mongo-express:
        image: mongo-express
        container_name: mongo-express
        command: /bin/sh -c "echo 'Waiting for mongodb...' && while ! nc -z mongodb 27017; do sleep 1; done && echo 'mongodb is ready.' && mongo-express"
        environment:
                   ...
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search