Search code examples
mongodbdockerdocker-composemongo-express

mongo-express is not starting with docker-compose file


Below is my docker-compose.yml file.

version: '3.1'
services:
  mongodb:
    image: mongo
    ports: 
      - 27017:27017
    environment:
      - MONGO_INITDB_ROOT_USERNAME=root
      - MONGO_INITDB_ROOT_PASSWORD=admin
  mongo-express:
    image: mongo-express
    ports:
      - 8081:8081
    environment:
      - ME_CONFIG_MONGODB_ADMINUSERNAME=root
      - ME_CONFIG_MONGODB_ADMINPASSWORD=admin
      - ME_CONFIG_MONGODB_SERVER=mongodb
    depends_on:
      - mongodb
    restart: on-failure

I'm getting error:

Could not connect to database using connectionString: mongodb://root:admin@mongodb:27017/" in mongo-express. PFA-1 The root user is created after error shown in mongo-express. PFA-2 is the issue because mongo-express executed before mongodb?

enter image description here

enter image description here


Solution

  • This is my code and it works properly:

        mongo:
          image: mongo
          environment:
             MONGO_INITDB_ROOT_USERNAME: root
             MONGO_INITDB_ROOT_PASSWORD: admin
          ports:
             - 27017
          volumes:
             - mongodb_data_container:/data/db
          command: mongod --port 27017 --auth
        
        mongo-express:
            image: mongo-express
            ports:
              - 8081:8081
            environment:
              ME_CONFIG_MONGODB_ADMINUSERNAME: root
              ME_CONFIG_MONGODB_ADMINPASSWORD: admin