Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run MongoDB and Mongo-express with docker-compose?

I try to run MongoDB and Mongo-express by Docker-compose. I use following config:

version: '3'

services:
    mongo:
        image: mongo
        environment:
            - MONGO_INITDB_ROOT_USERNAME=${MONGO_ROOT_USER}
            - MONGO_INITDB_ROOT_PASSWORD=${MONGO_ROOT_PASSWORD}
            - MONGO_INITDB_DATABASE=project
    mongo-express:
        image: mongo-express
        environment:
            - ME_CONFIG_MONGODB_SERVER=mongo
            - ME_CONFIG_MONGODB_PORT=27017
            - ME_CONFIG_MONGODB_ENABLE_ADMIN=false
            - ME_CONFIG_MONGODB_AUTH_DATABASE=admin
            - ME_CONFIG_MONGODB_AUTH_USERNAME=${MONGO_ROOT_USER}
            - ME_CONFIG_MONGODB_AUTH_PASSWORD=${MONGO_ROOT_PASSWORD}
            - ME_CONFIG_BASICAUTH_USERNAME=${MONGOEXPRESS_LOGIN}
            - ME_CONFIG_BASICAUTH_PASSWORD=${MONGOEXPRESS_PASSWORD}
        depends_on:
            - mongo
        ports:
          - "8080:8081"

and .env file:

MONGO_ROOT_USER=devroot
MONGO_ROOT_PASSWORD=devroot
MONGOEXPRESS_LOGIN=dev
MONGOEXPRESS_PASSWORD=dev

After docker-compose up mongo-service is running, but mongo-express fails. I see error in logs:

mongo-express_1  | Welcome to mongo-express
mongo-express_1  | ------------------------
mongo-express_1  | Mongo Express server listening at http://0.0.0.0:8081
mongo-express_1  | Server is open to allow connections from anyone (0.0.0.0)
mongo-express_1  | Database connected
mongo-express_1  | Connecting to admin...
mongo-express_1  | TypeError: db is not a function
mongo-express_1  |     at /node_modules/mongo-express/lib/db.js:114:53
mongo-express_1  |     at Array.forEach (native) 

What I do wrong? Thanks for advance!

like image 604
Sergey Krivochenko Avatar asked Dec 20 '17 08:12

Sergey Krivochenko


People also ask

Can I run MongoDB on docker?

MongoDB can be run in a Docker container. There is an official image available on Docker Hub containing the MongoDB community edition, used in development environments. For production, you may custom-build a container with MongoDB's enterprise version.

How do I connect to a MongoDB database in a docker container?

For connecting to your local MongoDB instance from a Container you must first allow to accept connections from the Docker bridge gateway. To do so, simply add the respective gateway IP in the MongoDB config file /etc/mongod. conf under bindIp in the network interface section.


2 Answers

Although this post is bit old. I would like to share the docker-compose.yml that worked for me.

The below spins up a mongoDB instance and mongo-express.

version: '3.7'
services:
  mongo_db:
    image: mongo:4.2.12
    environment:
      MONGO_INITDB_ROOT_USERNAME: root
      MONGO_INITDB_ROOT_PASSWORD: rootpassword
    ports:
      - 27017:27017
    volumes:
      - /C/Mine/mongoData:/data/db

  mongo_express:
    image: mongo-express:0.54.0
    environment:
      - ME_CONFIG_OPTIONS_EDITORTHEME=default
      - ME_CONFIG_MONGODB_SERVER=mongo_db
      - ME_CONFIG_MONGODB_PORT=27017
      - ME_CONFIG_MONGODB_ENABLE_ADMIN=true
      - ME_CONFIG_MONGODB_AUTH_DATABASE=mydb
      - ME_CONFIG_MONGODB_ADMINUSERNAME=root
      - ME_CONFIG_MONGODB_ADMINPASSWORD=rootpassword
    ports:
      - "8081:8081"
    restart: on-failure
    depends_on:
      - mongo_db
like image 108
Hari Rao Avatar answered Oct 03 '22 08:10

Hari Rao


This problem comes when your database uses a volumes configuration, but mongodb and mongo-express doesn't share a network configuration. This worked perfectly for me:

version: "3.7"

services:
  db:
    container_name: mongo-dev
    image: mongo:4.2
    environment:
      - MONGO_INITDB_ROOT_USERNAME=admin
      - MONGO_INITDB_DATABASE=auth
      - MONGO_INITDB_ROOT_PASSWORD=pass
    networks: 
      - mongo-compose-network
    ports:
      - '27017:27017'
    volumes: 
      - ./data:/data/db
  
  mongo-express:
    container_name: mongo-express
    image: mongo-express
    depends_on:
      - db
    networks: 
      - mongo-compose-network
    environment:
      - ME_CONFIG_MONGODB_SERVER=mongo-dev
      - ME_CONFIG_MONGODB_ADMINUSERNAME=admin
      - ME_CONFIG_MONGODB_ADMINPASSWORD=pass
      - ME_CONFIG_BASICAUTH_USERNAME=admin
      - ME_CONFIG_BASICAUTH_PASSWORD=tribes
    ports:
      - '8081:8081'
    volumes: 
      - ./data:/data/db      

networks:
  mongo-compose-network:
    driver: bridge
like image 35
Fonsito Avatar answered Oct 03 '22 10:10

Fonsito