MongoDB Docker Container Creation

MongoDB Docker Container Creation - This article is intended to introduce you to two different topics Docker and MongoDB and how MongoDB can be added as a Docker container on Docker so that the installation of MongoDB can be done on any system with much ease.

One thing to notice is that we are not performing this installation on a Windows machine, as Windows support for Docker is still in the budding stage and hence to save loads of time continued with Linux.

With no further delay, let us know to understand the semantics of Docker and MongoDB, and then we will look into the process of the creation of Docker Container in Docker.

What is Docker

Docker is a leading software container platform that helps developers to run and manage applications side by side in isolated containers for getting better to compute density. In simple words, docker will automate the repetitive mundane tasks of setting up the development machines/environments with the necessary configuration, so that the developers can work on the tedious job of creating better applications.

If you want to enrich your career and become a professional in MongoDB, then visit Mindmajix - a global online training platform: "MongoDB TrainingThis course will help you to achieve excellence in this domain. 

What is a Docker Container

A Docker container can be understood as a light-weight, stand-alone, executable package of the software that includes all that is needed to run/execute it.

Docker container’s main goal is to isolate software from its surroundings like for example the differences between development and staging environments running different software on the same infrastructure. A Docker container can be best understood as a virtual machine minus the operating system that comes along with a virtual machine.

How to Create a MongoDB Docker Container

Before creating a docker image of MongoDB, it is very important for us to understand how we configure our Docker image file. What details are required the most to have this achieved? I’m going to walk you through each step on how this can be achieved, starting with the docker image configuration file.

For creating the docker image, we are going to use a Fedora 20 instance with the docker-io package pre-installed. The docker instance is enabled and the docker service is UP and Running. Username has already been added to the docker group under /etc/group just so that we don’t have to issue sudo docker commands.

Defining the Container, that is the docker file: This is a Dockerfile for the MongoDB image in Fedora 20: The XXX can be replaced by your own IDs

MindMajix Youtube Channel

FROM fedora:20
MAINTAINER Mark Lamourine
#COMMENT { "description": "mongodb service container",
# "usage": "docker run -d -p 27017:27017 --name mongodb mlamouri/mongodb --volume=/mydbdatadir:/var/lib/mongodb" }
RUN  yum install -y mongodb-server && yum clean all
RUN  mkdir -p /var/lib/mongodb &&
    touch /var/lib/mongodb/.keep &&
    chown -R mongodb:mongodb /var/lib/mongodb
ADD mongodb.conf /etc/mongodb.conf
VOLUME [ "/var/lib/mongodb" ]
EXPOSE 27017
USER mongodb
WORKDIR /var/lib/mongodb
ENTRYPOINT ["/usr/bin/mongod", "--config", "/etc/mongodb.conf"]
CMD ["--quiet"]

Frequently Asked Docker Interview Questions

Want to Become an Expert in Docker Hub, Docker Compose, Docker Swarm

This is just what we need to create a MongoDB docker container image. Now it is very important to understand each and every line of the docker image configuration file, as a change to this will change the behavior of the docker container itself.

The explanation of the docker image configuration file has been adapted from the official Docker documentation.

  • FROM refers to an existing image on an Official public docker registry. Here, in our case, Fedora is maintaining this and notes that if you skip adding version 20, then the latest version image will be made available. It is very much important to keep what is needed in a docker file and skip what is not needed.
  • MAINTAINER refers to the maintainer of the image definition
  • RUN indicates the command that needs to be executed on the base image. This will make changes or adds things that will be captured and create a new layer. The shell command that follows the RUN directive should be treated as a single line or else the shell escapes the command once the line exceeds 80 characters.
  • This particular line installs the MongoDB-server package and after that, the YUM cache is cleared. Cleaning up after YUM eradicates chances of including cached RPMs and metadata from increasing the layer and thereby the image.
  • The next set of RUN command prepares the directories for the MongoDB storage
  • ADD directive adds the MongoDB.conf file to the container. This directive takes the file from the folder where the docker configuration file is residing and will overwrite the destination file on the container.
  • VOLUME directive is a declaration that the folder /var/lib/MongoDB will be used as a mount point for external data storage.
  • EXPOSE directive declares that the TCP port 21017 will be exposed for accepting connections from outside the container to access the hosted MongoDB instance inside it.
  • USER directive mentions that the first command executed will be under the MongoDB user’s context.
  • WORKDIR mentions that the command will run in /var/lib/MongoDB which is going to be the home directory for the MongoDB user here on.

CMD directive mentions the default command to execute when the container starts.

Checkout MongoDB Interview Questions

Building the Docker Image

With the Docker image configuration file and the MongoDB.conf template in the image directory, we are ready to build the image. Following is the transcript of the docker image creation.

docker build -t XXX/mongodb images/mongodb
Sending build context to Docker daemon 4.096 kB
Sending build context to Docker daemon
Step 0 : FROM fedora:20
Pulling repository fedora
88b42ffd1f7c: Download complete
511136ea3c5a: Download complete
c69cab00d6ef: Download complete
---> 88b42ffd1f7c
Step 1 : MAINTAINER Mark Lamourine
---> Running in 38db2e5fffbb
---> fc120ab67c77
Removing intermediate container 38db2e5fffbb
Step 2 : RUN  yum install -y mongodb-server &&      yum clean all
---> Running in 42e55f18d490
Resolving Dependencies
--> Running transaction check
---> Package mongodb-server.x86_64 0:2.4.6-1.fc20 will be installed
--> Processing Dependency: v8 for package: mongodb-server-2.4.6-1.fc20.x86_64
 mongodb-server.x86_64 0:2.4.6-1.fc20                                          

Cleaning repos: fedora updates
Cleaning up everything
---> 8924655bac6e
Removing intermediate container 42e55f18d490
Step 3 : RUN  mkdir -p /var/lib/mongodb &&      touch /var/lib/mongodb/.keep && shown -R mongodb:mongodb /var/lib/mongodb
---> Running in 88f5f059c3ff
---> f8e4eaed6105
Removing intermediate container 88f5f059c3ff
Step 4 : ADD mongodb.conf /etc/mongodb.conf
---> eb358bbbaf75
Removing intermediate container 090e1e36f7f6
Step 5 : VOLUME [ "/var/lib/mongodb" ]
---> Running in deb3367ff8cd
---> f91654280383
Removing intermediate container deb3367ff8cd
Step 6 : EXPOSE 27017
---> Running in 0c1d97e7aa12
---> 46157892e3fe
Removing intermediate container 0c1d97e7aa12
Step 7 : USER mongodb
---> Running in 70575d2a7504
---> 54dca617b94c
Removing intermediate container 70575d2a7504
Step 8 : WORKDIR /var/lib/mongodb
---> Running in 91759055c498
---> 0214a3fbcafc
Removing intermediate container 91759055c498
Step 9 : CMD [ "/usr/bin/mongod", "--quiet", "--config", "/etc/mongodb.conf", "run"]
---> Running in 6b48f1489a3e
---> 13d97f70aeb4
Removing intermediate container 6b48f1489a3e
Successfully built 13d97f70aeb4

A Docker container has a set of file trees that are layered using a read-only union filesystem with a read/write layer on the top. All the changes should go into the topmost layer.

When building a new image the changes for each directive are archived into a tarball and checksummed to produce the new layer and the layer's ID. This process is repeated for each directive, accumulating new layers until all of the directives have been processed.

The intermediate containers are deleted, the new layer files are saved and tagged. The end result is a new image (a set of new layers).

Checkout MongoDB Tutorial

The following is the simplest test that we can perform to check whether the newly created container is running or not.

docker run --name mongodb1 --detach --publish-all  XXX/mongodb

Running the command above indicates that the docker should run the image named XXX/MongoDB. This should actually detach and make all the network ports exposed by the container available to the host.

Names the container as mongodb1 just as we have mentioned it in the command above. The response to the command above is a hash of the currently running Docker container.

With the running container, we can safely assume that the MongoDB is running as well and waiting for a connection. Now how do we check that our service is up and running on the container? To list the running containers use docker ps.

docker ps

CONTAINER ID        IMAGE                      COMMAND                CREATED   
STATUS              PORTS                        NAMES

a90b275d00d4      XXX/mongodb:latest        /usr/bin/mongod --qu      5 mins ago
Up 5 min>27017/tcp     mongodb1

With the docker container name or its id, we can query for the required port information from the docker itself. The command which helps gain this information is shown as below:

docker port mongodb1 27017

The container port 23017 is forward to the host “all interfaces” port 49155. If the host firewall allows connections on that port, then the database could be compromised from outside

echo "show dbs" | mongo localhost:49155
MongoDB shell version: 3.4.7
connecting to: localhost:49155/test
local 0.03125GB


If you’ve reached this point, then you can verify that you have a running MongoDB instance accessible from the host. If it is required to have this instance accessible outside of the container, then that can be achieved as well.

In this article, we have understood the specifics of Docker and how a Docker container can be created. We have also spent some time understanding how MongoDB can exist as a single container on Docker. We have seen how this can be achieved on a Linux machine, as Windows support for Docker is still in the budding stage.

List Of MindMajix Docker Courses:

Course Schedule
MongoDB Training May 28 to Jun 12View Details
MongoDB Training Jun 01 to Jun 16View Details
MongoDB Training Jun 04 to Jun 19View Details
MongoDB Training Jun 08 to Jun 23View Details
Last updated: 03 Apr 2023
About Author

Prasanthi is an expert writer in MongoDB, and has written for various reputable online and print publications. At present, she is working for MindMajix, and writes content not only on MongoDB, but also on Sharepoint, Uipath, and AWS.

read less