MongoDB Docker Container Creation with Example

  • (4.0)
  •   |   571 Ratings

MongoDB Docker Container Creation - This article is intended to introduce you 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.

For an in-depth understanding and practical experience, Explore Online MongoDB Training.

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

What is Docker?

Docker is a leading software container platform which 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 of the development machines/environments with the necessary configuration, so that the developers can work on the tedious job of creating better applications.

Learn how to use Docker, from beginner basics to advanced techniques, with online video tutorials taught by industry experts. Enroll for Free Docker Training Demo!

What is a Docker Container?

A Docker container can be understood as a light-weight, stand-alone, executable package of software which 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 the 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 docker image configuration file.

For creating the docker image, we are going to use a Fedora 20 instance with 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 MongDB image in Fedora 20: The XXX can replaced by your own IDs

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 & Answers

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 note that if you skip adding the 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 which will be captured and creates 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 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 &&      chown -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 which 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.

Explore MongoDB Sample Resumes! Download & Edit, Get Noticed by Top Employers!Download Now!

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 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 to 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 spend some time in 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.

Explore Docker Sample Resumes! Download & Edit, Get Noticed by Top Employers!Download Now!

List Of MindMajix Docker Courses:

 Kubernetes Administration
 Docker Kubernetes
 OpenShift Administration

Popular Courses in 2018

Get Updates on Tech posts, Interview & Certification questions and training schedules