Among all the noSQL databases, MongoDB is the most famous document-oriented database that is used by many modern-day web applications. Docker ensures support for MongoDB by providing easy scalability and app usage by containers

Post Graduate Program: Full Stack Web Development

in Collaboration with Caltech CTMEEnroll Now
Post Graduate Program: Full Stack Web Development

Configure MongoDB as a Container in Docker

Docker is used as a toolset for creating, running, and deploying applications using containers. For packaging the applications and all the dependencies into a single package, a container is used which is a standard unit of software. Regardless of the hardware structure or the underlying configurations, these containers can run on any server platform. 

For running the MongoDB instances, Docker is used. The users are allowed to create extensible and portable noSQL databases if the MongoDb is set up as a container. Users do not need to worry about the underlying configurations at all because the containerized MongoDb instance is exactly like the non-containerized MongoDB instance 

Set Up the Docker Platform With Docker-Compose

Here, a simple Docker installation will be set up by us to run containers on an Ubuntu-based server. The packages for Docker installations can be obtained from the official Docker Repository

For installing Docker, we need to follow the steps given below:

  • For updating the existing packages, run the following command:

sudo apt update && sudo apt upgrade -y

  • For installing the prerequisite packages, run the command given below:

sudo apt install apt-transport-https ca-certificates curl software-properties-common

  • For adding the GPG key from the official Docker Repository, you need to run the following command:

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

  • For adding the official Docker repository to APT sources, the following command can be run:

sudo add-apt-repository \

"deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"

  • Ubuntu package list needs to be updated:

sudo apt update

  • Docker Repository needs to be verified:

apt-cache policy docker-ce

  • install the community edition of Docker:

sudo apt install docker-ce

  • The status of installation can be checked using the following command, if “running” is returned by the service status, we can say that Docker is installed successfully and it is also active on the system.

sudo systemctl status docker

Now let us go through the installation procedure for Docker Compose. 

MongoDB Developer and Administrator: Free Course

MongoDB Developer & Administrator Basics for FREEStart Learning
MongoDB Developer and Administrator: Free Course

Installing Docker Compose

For creating and managing the Docker containers, the Command Line Interface or CLI can be used. But for dealing with multiple containers and configurations, the CLI or Command Line Interface can be tedious sometimes. 

Docker can be used by the users for making multiple containers and integrating them into a single application. For creating the Docker compose files which can be easily executed using the compose-up and down commands, generally, YAML format is used by the Docker Compose. All the containers and the configurations within the compose file can be created or removed by those commands respectively.

Now for installing the Docker Compose on the Ubuntu server, we need to follow the steps given below.

  • First the current stable release of Docker compose needs to be installed:

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

  • For the downloaded binary, all the executable permissions are needed to be applied:

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

  • Then the installation of Docker Compose needs to be verified:

docker-compose --version

Now let us go through the setting up procedure of setting up the MongoDB container using the Docker Compose file.

Setting Up a MongoDB Container

The official MongoDB container image needs to be searched before creating the compose file, using search command:

sudo docker search mongodb

An official MongoDB container image will be shown by the search result saying mongo exists in the docker container registry. Within the data/db directory within the container, the databases are stored by the MongoDB container. 

Then for creating the Docker Compose file, a directory called “mongodb” needs to be created. After that, another directory called “database” will be created inside the MongoDB directory for mapping the database location of the container which will enable local access to the database. 

The -pv operator is used for creating parent folders.

>mkdir -pv mongodb/database

After that, to construct the MongoDB container, within the mongodb directory docker-compose.yml will be created. The created file will contain the following code:

version: "3.8"

services:

mongodb:

image : mongo

container_name: mongodb

environment:

- PUID=1000

- PGID=1000

volumes:

- /home/barry/mongodb/database:/data/db

ports:

- 27017:27017

restart: unless-stopped

For defining the user and the group of containers, the environment variables are generally used, and after that, the local port 27017 is mapped to the internal port 27017. Then for restarting, the restart policy is set.

MongoDB Developer and Administrator Course

Gain in-depth knowledge and become an expertEnroll now
MongoDB Developer and Administrator Course

You can check the file structure of the project using the following command:

tree mongodb

Now for starting the MongoDB container, first we need to go to the MongoDB folder and then run the docker-compose-up command.

sudo docker-compose up -d

In the above command, for running the detached container as the background process, the -d operator is used.

For pulling the mongo image from the Docker registry and for creating the containers using the given parameters in the docker-compose.yml file, the up command is used.

Now for verifying the running state of the container, the local folder is populated with the following commands:

sudo docker ps -a

For the command given, the -a operator is for displaying all the containers within the system regardless of their status.

Now let us discuss the interaction procedure with the MongoDB containers.

Interacting With the MongoDB Container

  • For accessing the terminal of the MongoDB command, the docker exec command can be used. The Docker interactive terminal is used for establishing the connection because the container runs in a detached mode.

sudo docker exec -it mongodb bash

  • Now the mongo command for accessing the MongoDB will be called in the bash terminal of the container. Along with the three documents, we will create a database called “food” and a database called “fruits”.

1. Switch database using the following command:

use food

2. Create collection:

db.createCollection("fruits")

3. Insert documents:

db.fruits.insertMany([ {name: "apple", origin: "usa", price: 5}, {name: "orange", origin: "italy", price: 3}, {name: "mango", origin: "malaysia", price: 3} ]   

4. Then the find command will be used for searching the documents:

      db.fruits.find().pretty()

5. For exiting both the MongoDB shell and the container shell, the exit command can be used.

Now let us discuss the external connections to the MongoDB container.

External Connections to MongoDB Container

  • The MongoDB port is mapped to the corresponding port in the server while creating the MongoDB container. This is how the MongoDB container is exposed to external networks. By simply pointing the mongo command to the appropriate port or server, the container can be connected from the external endpoint.

mongo 10.10.10.60:27017

  • To search for the collection called fruits and its documents for verifying that we are connected to a MongoDB container, the find command is used.

show databases

use food

show collections

db.fruits.find().pretty()

Now let us discuss data resilience.

Data Resilience

We have already mapped the database to the local folder. Therefore, if somehow the container is removed, the saved data in the local folder can be used for recreating a new MongoDB container. We’ll perform the following steps to test this theory:

  • The MongoDB container needs to be removed using the following command:

sudo docker-compose down

  • Remove the local mongo image using the following command:

sudo docker rmi mongo

  • Then verify the local database files 
  • Now after running the below command we can get confirmation that even after removing the containers, the data mapped to the local directory remains stored:

sudo tree mongodb

Now we will see the procedure for recreating the Mongodb container using the original docker-compose.yml life. 

  • Execute the following command in the MongoDB folder:

sudo docker-compose up -d

  • Again the data in the MongoDB container can be verified using the following command:

sudo docker exec -it mongodb bash

  • After that, we will get access to bash shell in the container and we will check the fruits collection:

show databases

use food

db.fruits.find().pretty()

  • The result that we get by running the above command indicates that the container was created with the local information 

Full Stack Web Developer Course

To become an expert in MEAN StackView Course
Full Stack Web Developer Course

Container Log FIles

We know that logs are created by every container and those logs are used for monitoring and debugging the container itself. Using the Docker log commands with the container name to be monitored, the container logs can be accessed by us.

sudo docker logs mongodb

Advance your career as a MEAN stack developer with the Full Stack Web Developer - MEAN Stack Master's Program. Enroll now!

Conclusion

We hope this article helped you understand the concept of MongoDB Docker better. In this article, we saw how to run MongoDB as a Docker Container and learned how to manage MongoDB on docker with docker-compose. 

We also learned that the database development process can be shortened and can be simplified if we use Docker and MongoDB container images. 

To know more about MongoDB Docker, you can enroll in the Full-Stack Web Development program offered by Simplilearn in collaboration with Caltech CTME. This Web Development course is a descriptive online bootcamp with interactive online classes. In addition to Docker MySQL, the course also details everything you need to become a full-stack technologist and accelerate your career as a software developer.

Simplilearn also offers free online skill-up courses in several domains, from data science and business analytics to software development, AI, and machine learning. You can take up any of these free courses to upgrade your skills and advance your career.

About the Author

SimplilearnSimplilearn

Simplilearn is one of the world’s leading providers of online training for Digital Marketing, Cloud Computing, Project Management, Data Science, IT, Software Development, and many other emerging technologies.

View More
  • Disclaimer
  • PMP, PMI, PMBOK, CAPM, PgMP, PfMP, ACP, PBA, RMP, SP, and OPM3 are registered marks of the Project Management Institute, Inc.