Docker

Set up a MongoDB server with Docker

In this article, I am going to show you how to use Docker Compose to create a MongoDB container and access it using Mongo Express, the web-based MongoDB admin interface. So, let’s get started.

Requirements:

In order to follow this article, you must have Docker installed on your computer. LinuxHint has a lot of articles that you can follow to install Docker on your desired Linux distribution if you don’t have it installed already. So, be sure to check LinuxHint.com in case you’re having trouble installing Docker.

Installing Docker Compose:

To install Docker Compose, open a Terminal and run the following command:

$ sudo curl -L "https://github.com/docker/compose/releases/download/1.24.1/
docker-compose-$(uname -s)-$(uname -m)"
-o /usr/local/bin/docker-compose

CURL should start downloading Docker Compose binary. It may take a while to complete.

Once the download is complete, add executable permission to the docker-compose binary file with the following command:

$ sudo chmod +x /usr/local/bin/docker-compose

Now, check if docker-compose binary is accessible with the following command:

$ docker-compose --version

As you can see, docker-compose binary is working correctly.

Setting Up MongoDB Server using Docker Compose:

In this section, I will show you a very basic MongoDB and Mongo Express setup using Docker Composer.

First, create a new project directory mongo-latest/ as follows:

$ mkdir mongo-latest

Now, navigate to the mongo-latest/ directory as follows:

$ cd mongo-latest/

Now, create a new Docker Compose configuration file docker-compose.yml as follows:

$ nano docker-compose.yml

Now, type in the following lines of codes in the file docker-compose.yml.

version: '3'
services:
mongo:
image: mongo
restart: always
ports:
- "27017:27017"
 
mongo-express:
image: mongo-express
restart: always
ports:
- "8081:8081"

Here, we are defining two services, mongo and mongo-express. The mongo service is responsible for starting the mongo (image) Docker container. The mongo Docker container port 27017 is forwarded to the port 27017 on your Docker host. The mongo-express service is responsible for starting the mongo-express (image) Docker container. The same way, port 8081 is forwarded to port 8081 on the Docker host.

Both the services are set to restart (restart: always) on failure.

Now, save the file by pressing <Ctrl> + X followed by Y and <Enter>.

Now, to start the MongoDB and Mongo Express services, run the following command:

$ docker-compose up -d

If you’re running this command for the first time, it may take a while to start the services as the Docker images will be downloaded from Docker Hub.

The services should start.

Now, from a web browser, visit http://localhost:8081 and the Mongo Express web interface should be displayed as you can see in the screenshot below. From here, you can manage MongoDB databases, add data to the database and many more.

Setting Up MongoDB Database Password:

If you want to set up a MongoDB database password, then this section is for you.

First, stop the MongoDB and Mongo Express services as follows:

$ docker-compose down

Now, edit the docker-compose.yml file as follows:

$ nano docker-compose.yml

Now, add the marked text (bold) below to the configuration file docker-compose.yml.

version: '3'
services:
mongo:
image: mongo
restart: always
ports:
- "27017:27017"
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: secret
mongo-express:
image: mongo-express
restart: always
ports:
- "8081:8081"
environment:
ME_CONFIG_MONGODB_ADMINUSERNAME: root
ME_CONFIG_MONGODB_ADMINPASSWORD: secret

The MONGO_INITDB_ROOT_USERNAME: root and MONGO_INITDB_ROOT_PASSWORD: secret in the mongo service section are used to set the MongoDB username root and password secret.

The ME_CONFIG_MONGODB_ADMINUSERNAME: root and ME_CONFIG_MONGODB_ADMINPASSWORD: secret in the mongo-express service section are used to tell Mongo Express to use the username root and password secret to connect to the MongoDB server.

Final configuration file.

Now, start the services as follows:

$ docker-compose up -d

As you can see, I can still access the Mongo Express web interface.

I changed the mongo-express password to something other than secret, and as you can see, I was not able to connect to the MongoDB server. So, everything is working as expected.

Saving MongoDB Data Using Volumes:

By default, MongoDB container do not save any data. If you want your MongoDB database data to persist, you have to use Docker volumes. I will show you how in this section.

First, stop the services as follows:

$ docker-compose down

Now, add the marked (bold) section to the docker-compose.yml file.

version: '3'
services:
mongo:
image: mongo
restart: always
ports:
- "27017:27017"
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: secret
volumes:
- mongo-data:/data/db
 
mongo-express:
image: mongo-express
restart: always
ports:
- "8081:8081"
environment:
ME_CONFIG_MONGODB_ADMINUSERNAME: root
ME_CONFIG_MONGODB_ADMINPASSWORD: secret
volumes:
 mongo-data:

Here, I defined a new named volume mongo-data which saves the data from the path /data/db of the mongo container.

Final configuration file.

Now, start the services as follows:

$ docker-compose up -d

As you can see, the Mongo Express web interface is working correctly.

I created a new database mydb.

Now, restart the services as follows:

$ docker-compose restart

As you can see, even after restarting the mongo (MongoDB) and mongo-express (Mongo Express) services the mydb database is still available. So, it worked.

Changing MongoDB Container Name:

By default, the MongoDB database service name should be mongo in order for Mongo Express to work. But, if you want to change the database service name to something else like mongodb-server, then you will have to tell the mongo-express service that you’ve changed the mongo service name to something else.

First, stop the services as follows:

$ docker-compose down

Now, change/add the marked text in the docker-compose.yml configuration file.

version: '3'
services:
mongodb-server:
image: mongo
restart: always
ports:
- "27017:27017"
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: secret
volumes:
- mongo-data:/data/db
 
mongo-express:
image: mongo-express
restart: always
ports:
- "8081:8081"
environment:
ME_CONFIG_MONGODB_ADMINUSERNAME: root
ME_CONFIG_MONGODB_ADMINPASSWORD: secret
ME_CONFIG_MONGODB_SERVER: mongodb-server
volumes:
mongo-data:

Here, ME_CONFIG_MONGODB_SERVER: mongodb-server is used to tell the mongo-express service that the mongo service name is changed to mongodb-server.

The final configuration file.

Now, start the services as follows:

$ docker-compose up -d

As you can see, the Mongo Express web interface is working still.

Setting Mongo Express Access Password:

By default, the Mongo Express admin interface does not ask you for any login information. But if you want to set up a username and password authentication method, then this section is for you.

First, stop the services as follows:

$ docker-compose down

Now, add the marked (bold) text to the docker-compose.yml configuration file.

version: '3'
services:
mongo:
image: mongo
restart: always
ports:
- "27017:27017"
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: secret
volumes:
- mongo-data:/data/db
 
mongo-express:
image: mongo-express
restart: always
ports:
- "8081:8081"
environment:
ME_CONFIG_MONGODB_ADMINUSERNAME: root
ME_CONFIG_MONGODB_ADMINPASSWORD: secret
ME_CONFIG_BASICAUTH_USERNAME: admin
ME_CONFIG_BASICAUTH_PASSWORD: secret
volumes:
mongo-data:

Here, ME_CONFIG_BASICAUTH_USERNAME: admin and ME_CONFIG_BASICAUTH_PASSWORD: secret is used to tell mongo-express service to use the username admin and the password secret for authentication.

Final configuration file.

Now, start the services as follows:

$ docker-compose up -d

Now, if you try to visit the Mongo Express page, you will be asked to authenticate using your username and password.

If you provide the correct username and password, you will be allowed to use Mongo Express to manage your MongoDB database.

References:

For more information, check the following links:
[1] Official Docker Hub page of mongo
[2] Official Docker Hub page of mongo-express

So, that’s how you set up a MongoDB server using Docker. Thanks for reading this article.

About the author

Shahriar Shovon

Freelancer & Linux System Administrator. Also loves Web API development with Node.js and JavaScript. I was born in Bangladesh. I am currently studying Electronics and Communication Engineering at Khulna University of Engineering & Technology (KUET), one of the demanding public engineering universities of Bangladesh.