Home > Mobile >  mongodb and mongo-express not connecting wtih docker-compose
mongodb and mongo-express not connecting wtih docker-compose

Time:11-15

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

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