3

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

J. Scott Elblein
  • 4,013
  • 15
  • 58
  • 94

2 Answers2

4

For me it was this part that made the mongo express start:

mongo-express:
    image: mongo-express
    restart: always
    ...
John
  • 43
  • 5
0

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
alexmark
  • 369
  • 4
  • 20