'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 1:[1]

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

Solution 2:[2]

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

mongo-express:
    image: mongo-express
    restart: always
    ...

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 alexmark
Solution 2 John