October 21, 2021

How To Deploy MongoDB on Docker Community Edition

On this article we will discuss how to deploy MongoDB Community Edition on Docker Community Edition version 19.03.

Introduction

MongoDB is the most popular NoSQL database today and is widely used by various organizations in the world. On the other hand, Docker is a very lightweight and powerful containerize tool that allows applications to run efficiently. Using Docker and an official MongoDB container image can significantly simplify the database deployment process. On this article we will discuss how to deploy MongoDB Community Edition on Docker Community Edition. This article is one of the references before we deploy MongoDB on Docker it in a production system.

The article will be divided into several subsection, namely :

  1. Prerequisites
  2. Download MongoDB Image for Docker
  3. Deploy MongoDB Container
  4. Test Interactive Docker Terminal to Manage MongoDB Database

Prerequisite

Before deploying MongoDB on Docker, there are several thing we have to meet its requirements as mention below :

  • A system with running Docker instance
  • A user with sudo privileges
  • Sufficient disk space and good network connection

The Docker instance on our system, can be checked by querying its version and service. Is it running properly on our CentOS or not. We will check it by submitting command line : docker --version and sudo systemctl status docker.

[ramans@diginetdb01 ~]$ sudo systemctl status docker
[sudo] password for ramans:
● docker.service - Docker Application Container Engine
Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled; vendor preset: disabled)
Active: active (running) since Fri 2020-11-27 07:25:18 PST; 3min 46s ago
Docs: https://docs.docker.com
Main PID: 1283 (dockerd)
Tasks: 13
Memory: 127.5M
CGroup: /system.slice/docker.service
└─1283 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
Nov 27 07:25:14 diginetdb01 dockerd[1283]: time="2020-11-27T07:25:14.759020047-08:00" level=info msg="[graphdriver] u>
Nov 27 07:25:14 diginetdb01 dockerd[1283]: time="2020-11-27T07:25:14.867132784-08:00" level=warning msg="Your kernel >
Nov 27 07:25:14 diginetdb01 dockerd[1283]: time="2020-11-27T07:25:14.867246737-08:00" level=warning msg="Your kernel >
Nov 27 07:25:14 diginetdb01 dockerd[1283]: time="2020-11-27T07:25:14.868924066-08:00" level=info msg="Loading contain>
Nov 27 07:25:17 diginetdb01 dockerd[1283]: time="2020-11-27T07:25:17.330724577-08:00" level=info msg="Default bridge >
Nov 27 07:25:17 diginetdb01 dockerd[1283]: time="2020-11-27T07:25:17.673534828-08:00" level=info msg="Loading contain>
Nov 27 07:25:17 diginetdb01 dockerd[1283]: time="2020-11-27T07:25:17.896814037-08:00" level=info msg="Docker daemon" >
Nov 27 07:25:17 diginetdb01 dockerd[1283]: time="2020-11-27T07:25:17.900375470-08:00" level=info msg="Daemon has comp>
Nov 27 07:25:18 diginetdb01 dockerd[1283]: time="2020-11-27T07:25:18.002911306-08:00" level=info msg="API listen on />
Nov 27 07:25:18 diginetdb01 systemd[1]: Started Docker Application Container Engine.

It seems if the Docker engine has been activated on the system. Then we could proceed the next step : Download MongoDB image to the Docker.

Download MongoDB Image

For this purpose we will chose the latest official Docker image for the MongoDB database. We will submit the command line : sudo docker pull mongo.

[ramans@diginetdb01 ~]$ sudo docker pull mongo
Using default tag: latest
latest: Pulling from library/mongo
f22ccc0b8772: Pull complete
3cf8fb62ba5f: Pull complete
e80c964ece6a: Pull complete
329e632c35b3: Pull complete
3e1bd1325a3d: Pull complete
4aa6e3d64a4a: Pull complete
035bca87b778: Pull complete
874e4e43cb00: Pull complete
08cb97662b8b: Pull complete
f623ce2ba1e1: Pull complete
f100ac278196: Pull complete
b16ea696739f: Pull complete
Digest: sha256:d54fb51a493131117a8b85a2d46ed7d64a2cb79607fb3ce350d1722c0cafc812
Status: Downloaded newer image for mongo:latest
docker.io/library/mongo:latest
docker pull mongo

After pulling the latest mongoDB repository, then we will list all repository which were attached. We will submit the command line : sudo docker images, as shown below :

[ramans@diginetdb01 ~]$ sudo docker images
[sudo] password for ramans:
REPOSITORY TAG IMAGE ID CREATED SIZE
mongo latest ef5c2207766e 38 hours ago 493MB
hello-world latest bf756fb1ae65 10 months ago 13.3kB
list dokcer images

Deploy MongoDB Container

On this step, we will deploy MongoDB container on Docker. MongoDB will use /data/db directory to hold the data, by default. So we will create /data/db directory then will be assigned as /mongodb on container. Setting MongoDB container will be consist of several steps as shown below :

  1. Create /mongodbdata directory by submitting command line below :
    sudo mkdir -p /mongodbdata
  2. Start the Docker container with the run command using the mongo image, by typing :
    sudo docker run -it -v mongodbdata:/data/db --name mongodb -d mongo
  3. Check the status contaniner status by typing:
    sudo docker ps
  4. Check the Docker log to see the chain of events after making changes, by submitting command line below :
    sudo docker logs mongodb
[ramans@diginetdb01 ~]$ sudo mkdir -p /mongodbdata
[ramans@diginetdb01 ~]$ sudo docker run -it -v mongodbdata:/data/db --name mongodb -d mongo
61353327f47bd73e1a6f3e333adadf5ac87fb6659a796cc4d97f927e08afb1b1
[ramans@diginetdb01 ~]$ sudo docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
61353327f47b mongo "docker-entrypoint.s…" 24 seconds ago Up 23 seconds 27017/tcp mongodb
Creating MongoDB Container on Docker
Creating MongoDB Container on Docker

5. We should always check the Docker log to see what happened on the container after making changes, by submitting command line below :
sudo docker logs mongodb

[ramans@diginetdb01 ~]$ sudo docker logs mongodb
{"t":{"$date":"2020-11-27T15:50:15.670+00:00"},"s":"I", "c":"CONTROL", "id":23285, "ctx":"main","msg":"Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols 'none'"}
{"t":{"$date":"2020-11-27T15:50:15.678+00:00"},"s":"W", "c":"ASIO", "id":22601, "ctx":"main","msg":"No TransportLayer configured during NetworkInterface startup"}
{"t":{"$date":"2020-11-27T15:50:15.679+00:00"},"s":"I", "c":"NETWORK", "id":4648601, "ctx":"main","msg":"Implicit TCP FastOpen unavailable. If TCP FastOpen is required, set tcpFastOpenServer, tcpFastOpenClient, and tcpFastOpenQueueSize."}
{"t":{"$date":"2020-11-27T15:50:15.681+00:00"},"s":"I", "c":"STORAGE", "id":4615611, "ctx":"initandlisten","msg":"MongoDB starting","attr":{"pid":1,"port":27017,"dbPath":"/data/db","architecture":"64-bit","host":"61353327f47b"}}
{"t":{"$date":"2020-11-27T15:50:15.681+00:00"},"s":"I", "c":"CONTROL", "id":23403, "ctx":"initandlisten","msg":"Build Info","attr":{"buildInfo":{"version":"4.4.2","gitVersion":"15e73dc5738d2278b688f8929aee605fe4279b0e","openSSLVersion":"OpenSSL 1.1.1 11 Sep 2018","modules":[],"allocator":"tcmalloc","environment":{"distmod":"ubuntu1804","distarch":"x86_64","target_arch":"x86_64"}}}}
{"t":{"$date":"2020-11-27T15:50:15.681+00:00"},"s":"I", "c":"CONTROL", "id":51765, "ctx":"initandlisten","msg":"Operating System","attr":{"os":{"name":"Ubuntu","version":"18.04"}}}
{"t":{"$date":"2020-11-27T15:50:15.681+00:00"},"s":"I", "c":"CONTROL", "id":21951, "ctx":"initandlisten","msg":"Options set by command line","attr":{"options":{"net":{"bindIp":"*"}}}}
docker logs mongodb
Docker logs Mongodb

Test Interactive Docker Terminal to Manage MongoDB Database

So far, the MongoDB container was live on our Docker. The next step is login in and trying to configure our MongoDB on interactive mode. For this purpose we will submit the command line : sudo docker exec -it mongodb bash as shown below.

[ramans@diginetdb01 ~]$ sudo docker exec -it mongodb bash
root@61353327f47b:/# mongo
MongoDB shell version v4.4.2
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("43c840dd-93a9-4d85-9fa7-47816f35b029") }
MongoDB server version: 4.4.2
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
https://docs.mongodb.com/
Questions? Try the MongoDB Developer Community Forums
https://community.mongodb.com
The server generated these startup warnings when booting:
2020-11-27T15:50:16.896+00:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
2020-11-27T15:50:16.896+00:00: /sys/kernel/mm/transparent_hugepage/enabled is 'always'. We suggest setting it to 'never'
---
Enable MongoDB's free cloud-based monitoring service, which will then receive and display
metrics about your deployment (disk utilization, CPU, operation statistics, etc).

The monitoring data will be available on a MongoDB website with a unique URL accessible to you
and anyone you share the URL with. MongoDB may use this information to make product
improvements and to suggest MongoDB products and deployment options to you.

To enable free monitoring, run the following command: db.enableFreeMonitoring()
To permanently disable this reminder, run the following command: db.disableFreeMonitoring()
---
Login to MongoDB on Docker

On this example we have logged in, and show database. And then trying to create new database called as otodiget_db and created the collection as subsicribers.

> show databases;
admin 0.000GB
config 0.000GB
local 0.000GB
> use otodiget_db
switched to db otodiget_db
> db.getName()
otodiget_db
> db.createCollection("subscribers")
{ "ok" : 1 }
> show databases;
admin 0.000GB
config 0.000GB
local 0.000GB
otodiget_db 0.000GB
>exit
bye
Collection creatoin on MongoDB

Share this article via :

Leave a Reply

Your email address will not be published. Required fields are marked *