Recipe : Running mongodb and mongo-express in Docker and initializing databases on startup

Printer-friendly versionPDF version

This is a recipe to setup a mongodb container for your development purposes or otherwise. In production donot use mongo-express at all. The database intialization is tricky but if you follow this guide you'll be done with some leeway for customizations in a matter of minutes.
So this is how your docker-compose.yml file is supposed should look like:

version: '3.4'
 
services:
    ahsanscorner-mongodb:
        image: mongo:4.1.8-xenial
        container_name: "ahsanscorner-mongodb"
        env_file:
            mongodb.env        
        volumes:
            - mongodb-data-volume:/data/db
            - /opt/ahsanscorner/develop/ahsanscorner-mongodb/docker-entrypoint-initdb.d:/docker-entrypoint-initdb.d
        ports:
            - "27017:27017"
        command:
            - '--auth'
 
########################################################################
############## Mongo Express service is not recommened #################
############## to start in production environment.     #################
########################################################################
 
    ahsanscorner-mongo-express:
        image: mongo-express
        container_name: "ahsanscorner-Mongo-Express"
        env_file:
            mongo-express.env
        ports:
            - "9092:8081"
        links:
            # set service's alias as mongo while mongo express expects this name unless 
            # you set the correct container name on env var ME_CONFIG_MONGODB_SERVER
            - ahsanscorner-mongodb:mongo
 
volumes:
    mongodb-data-volume:
        name: mongodb-data-volume

To go along with the above you need to create 2 env files in the same folder or location as the docker-compose.yml file.
The first file is called "mongodb.env" and the contents should look like this. You can add additional environment variables here to expand on your needs:

#### Mongo DB variables #### 
 
# Creating a simple user with the role root in the admin authentication database
MONGO_INITDB_ROOT_USERNAME=admin
MONGO_INITDB_ROOT_PASSWORD=admin

The 2nd file is needed for mongo-express and should look like this:

#### Mongo express variables #### 
 
ME_CONFIG_MONGODB_ADMINUSERNAME=admin
ME_CONFIG_MONGODB_ADMINPASSWORD=admin
ME_CONFIG_MONGODB_SERVER=ahsanscorner-mongodb
ME_CONFIG_MONGODB_PORT=27017
 
# This part gives you admin login rights of the complete MongoDB
ME_CONFIG_MONGODB_ENABLE_ADMIN=true
 
# Request size of Mongo Express and not mongodb
ME_CONFIG_REQUEST_SIZE=10000kb

Now you need to create the entrypoint folder that you are going to mount with the user creation script in the mongodb container. Create a folder "docker-entrypoint-initdb.d" on the host. In my case the location is "/opt/ahsanscorner/develop/ahsanscorner-mongodb/". This folder is going to be mounted to the folder "docker-entrypoint-initdb.d" im root folder on the mongodb container. Create a file or as many files as you want, lets say arbitarity with naming convention "create_user_database1.js" and "create_user_database2.js" and so on for as many databases that you want created for your mongodb on startup.
The content of the "create_user_databaseX.js" is something that caused some amount of pain but if you do as below, you'll avoid this pain:

db = db.getSiblingDB('admin')
db.auth('admin', 'admin')
 
db = db.getSiblingDB('databaseX')
db.createUser(
    {
        user: "databaseX",
        pwd: "databaseX",
        roles: [
            { role: "readWrite", db: "databaseX" }
        ]
    }
)

The mongodb container documentation is very vague on this script. So databaseX is your database name and username password as also set as same. "admin" is the username your specified as environment variable, as well as "admin" as password on the environment file. The databases get created only once at first startup. As long as you use the same volume, where initialization has happened for e.g. "mongodb-data-volume", on next startup no intialization will happen, as documented by the mongodb official docker page. You can delete the volume or put in a new name in the docker-compose file and you can start from scratch with new db container and data. Just dont let those volumes grow on you.

Add new comment