Home > Net >  mongodb and mongo-express not connecting with docker-compose
mongodb and mongo-express not connecting with docker-compose

Time:11-16

I'm new to learning docker and got stuch here. This is my compose.yaml file which has mongo and mongo-express latest versions use case. The mongo-express doesn't get connected to the mongodb, tried the restart functionality in mongo-express which keep on restarting mongo-express which I suffice it's not getting connected with mongodb. I tried giving giving the network doesn't change anything.

version: "3"
services:
  mongodb:
    image: mongo
    ports:
      - "27017:27017"
    environment:
      - MONGO_INITDB_ROOT_USERNAME=admin
      - MONGO_INITDB_ROOT_PASSWORD=password
    # networks:
    #   - mongo-network
  mongo-express:
    image: mongo-express
    # restart: on-failure
    ports:
      - "8081:8081"
    environment:
      - ME_CONFIG_MONGODB_ADMINUSERNAME=admin
      - ME_CONFIG_MONGODB_ADMINPASSWORD=password
      - ME_CONFIG_MONGODB_ADMINSERVER=mongodb
      - ME_CONFIG_MONGODB_PORT=27017
    # networks:
    #   - mongo-network
    depends_on:
      - mongodb

This is the error I'm getting in my command prompt from the mongo-express container:

2022-11-15 10:39:21 Welcome to mongo-express
2022-11-15 10:39:21 ------------------------
2022-11-15 10:39:21 
2022-11-15 10:39:21 
2022-11-15 10:39:21 (node:7) [MONGODB DRIVER] Warning: Current Server Discovery and Monitoring engine is deprecated, and will be removed in a future version. To use the new Server Discover and Monitoring engine, pass option { useUnifiedTopology: true } to the MongoClient constructor.
2022-11-15 10:39:26 Could not connect to database using connectionString: mongodb://mongo:27017"
2022-11-15 10:39:26 (node:7) UnhandledPromiseRejectionWarning: MongoNetworkError: failed to connect to server [mongo:27017] on first connect [Error: getaddrinfo EAI_AGAIN mongo
2022-11-15 10:39:26     at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:66:26) {
2022-11-15 10:39:26   name: 'MongoNetworkError'
2022-11-15 10:39:26 }]
2022-11-15 10:39:26     at Pool.<anonymous> (/node_modules/mongodb/lib/core/topologies/server.js:441:11)
2022-11-15 10:39:26     at Pool.emit (events.js:314:20)
2022-11-15 10:39:26     at /node_modules/mongodb/lib/core/connection/pool.js:564:14
2022-11-15 10:39:26     at /node_modules/mongodb/lib/core/connection/pool.js:1000:11
2022-11-15 10:39:26     at /node_modules/mongodb/lib/core/connection/connect.js:32:7
2022-11-15 10:39:26     at callback (/node_modules/mongodb/lib/core/connection/connect.js:300:5)
2022-11-15 10:39:26     at Socket.<anonymous> (/node_modules/mongodb/lib/core/connection/connect.js:330:7)
2022-11-15 10:39:26     at Object.onceWrapper (events.js:421:26)
2022-11-15 10:39:26     at Socket.emit (events.js:314:20)
2022-11-15 10:39:26     at emitErrorNT (internal/streams/destroy.js:92:8)
2022-11-15 10:39:26     at emitErrorAndCloseNT (internal/streams/destroy.js:60:3)
2022-11-15 10:39:26     at processTicksAndRejections (internal/process/task_queues.js:84:21)
2022-11-15 10:39:26 (node:7) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
2022-11-15 10:39:26 (node:7) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

CodePudding user response:

The server env var is not ME_CONFIG_MONGODB_ADMINSERVER but ME_CONFIG_MONGODB_SERVER as per the documentation: https://hub.docker.com/_/mongo-express

Also you need to add a healthcheck for mongodb and make the mongo-express depend on it because node is exiting with exit code 0 after the first failed connection as it says in the log. If it would exit with non-zero exit code docker would try to restart the container as long as it is not failing. It says in the log that this will be the way in the future, but for now you need the healthcheck.

(node:7) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

So this compose file works:

version: "3"
services:
  mongodb:
    image: mongo
    ports:
      - "27017:27017"
    environment:
      - MONGO_INITDB_ROOT_USERNAME=admin
      - MONGO_INITDB_ROOT_PASSWORD=password
    healthcheck:
      test:  echo 'db.runCommand("ping").ok' | mongosh localhost:27017/test --quiet
      interval: 10s
      timeout: 10s
      retries: 5
      start_period: 10s
  mongo-express:
    image: mongo-express
    # restart: on-failure
    ports:
      - "8081:8081"
    environment:
      - ME_CONFIG_MONGODB_ADMINUSERNAME=admin
      - ME_CONFIG_MONGODB_ADMINPASSWORD=password
      - ME_CONFIG_MONGODB_SERVER=mongodb
      - ME_CONFIG_MONGODB_PORT=27017
    depends_on:
      mongodb:
        condition: service_healthy
  • Related