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.
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 Training" This course will help you to achieve excellence in this domain.
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.
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
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
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.
CMD directive mentions the default command to execute when the container starts.
Checkout MongoDB Interview Questions
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
...
Installed:
mongodb-server.x86_64 0:2.4.6-1.fc20
...
Complete!
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).
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
e90b275d00d451fda4add9bc99798e4487815e38c8efbe51bfde505c17d920eb
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 0.0.0.0:49155->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
0.0.0.0:49155
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
bye
Conclusion
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.
Our work-support plans provide precise options as per your project tasks. Whether you are a newbie or an experienced professional seeking assistance in completing project tasks, we are here with the following plans to meet your custom needs:
Name | Dates | |
---|---|---|
MongoDB Training | Dec 21 to Jan 05 | View Details |
MongoDB Training | Dec 24 to Jan 08 | View Details |
MongoDB Training | Dec 28 to Jan 12 | View Details |
MongoDB Training | Dec 31 to Jan 15 | View Details |
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.