Introduction
MongoDB is a practical NoSQL database solution. It does not use a fixed data structure, making it scalable and ideal for managing dynamic workloads. MongoDB is well suited for distributed environments, such as Docker containers.
Using Docker and an official MongoDB container image can significantly shorten and simplify the database deployment process.
This tutorial will show you how to deploy a MongoDB instance on a Docker container.
Prerequisites
- A user with sudo privileges
- Access to a command line
- A running Docker instance
Download MongoDB Image for Docker
Follow the step-by-step instructions below to download the latest official MongoDB image for Docker.
- Your Docker service needs to be active and running. You can quickly check the current status by entering the following command in your terminal:
sudo service docker status
In this example, the Docker service is active and running.
- Proceed to download the latest official Docker image for the MongoDB database:
sudo docker pull mongo
The image indicates that the system used the latest
tag by default.
To download a specific version of MongoDB, use the same command appended with the version tag. For example:
sudo docker pull mongo:4.2.2
- List the images in your Docker repository with the following command:
sudo docker images
The interface confirms that the MongoDB image is now available.
Deploy MongoDB Container
By default, MongoDB stores data in the /data/db directory within the Docker container. To remedy this, mount a directory from the underlying host system to the container running the MongoDB database. This way, data is stored on your host system and is not going to be erased if a container instance fails.
- Create a /mongodata directory on the host system:
sudo mkdir -p /mongodata
- Start the Docker container with the
run
command using the mongo image. The /data/db directory in the container is mounted as /mongodata on the host. Additionally, this command changes the name of the container to mongodb:
sudo docker run -it -v mongodata:/data/db --name mongodb -d mongo
-it
– Provides an interactive shell to the Docker container.
-v
– Use this option to attach the /mongodata host volume to the /data/db container volume.
-d
– Starts the container as a background process.
--name
– Name of the container.
- Once the MongoDB server starts running in a container, check the status by typing:
sudo docker ps
The default port number is 27017, as can be seen in the output.
- Optionally you can specify the MongoDB port explicitly:
sudo docker run -it -v mongodata:/data/db -p 27017:27017 --name mongodb -d mongo
- Always check the Docker log to see the chain of events after making changes:
sudo docker logs mongodb
The logs provide a wealth of useful information.
Start Interactive Docker Terminal (Bash Shell) to Manage MongoDB Database
- The container is currently running in detached mode. Connect to the container using the interactive terminal instead:
sudo docker exec -it mongodb bash
- Start the MongoDB shell by typing
mongo
in the interactive terminal.
The MongoDB shell launches and the prompt is ready to accept your commands.
- Instead of just typing
mongo
, you can additionally define a specific host and port by typing:
mongo -host localhost -port 27017
With the MongoDB shell, you can now create a database, add collections or manage individual documents.
How to Exit MongoDB and Interactive Shell
Type exit
to leave the MongoDB shell and then exit
once again to leave the Interactive shell.
As an alternative, you can type quit()
or use Ctrl-C to exit the shell.
Stopping and Restarting MongoDB Database
The docker stop
command is a short and clear command that stops running container instances:
sudo docker stop mongodb
Inspect the list of running Docker containers by typing:
sudo docker ps
Containers are started by using the docker start
command:
sudo docker start mongodb
The list of running containers now confirms that the MongoDB database has been initiated once again:
sudo docker ps
Conclusion
You now know how to install MongoDB on a Docker container, and you have learned how to access the MongoDB shell to manage databases.
Use Docker to streamline MongoDB database deployment across multiple servers and scale your operations quickly and efficiently.
Next you should also read
How to Install MongoDB on CentOS 8
July 15, 2020
MongoDB allows storing different fields in different documents, without a permanently fixed data structure.…
How to Create Database & Collection in MongoDB
April 29, 2020
Create new MongoDB databases and add data collections by using the commands presented in this article. The…
DevOps and Development,Virtualization
List of Docker Commands: Cheat Sheet
December 2, 2019
Docker has earned a reputation as one of the most popular open-source platforms for application development.…
How to Install MongoDB on Ubuntu 18.04
November 26, 2019
MongoDB is a database program that provides high performance, high availability, and automatic scaling to…
How to Install & Setup MEAN Stack on Ubuntu (MongoDB, Express.JS, Angular.JS, Node.JS)
April 9, 2019
The MEAN stack is an open-source JavaScript (JS) framework used for developing robust web applications. It is…
Author
Vladimir Kaplarevic
Vladimir is a resident Tech Writer at phoenixNAP. He has more than 7 years of experience in implementing e-commerce and online payment solutions with various global IT services providers. His articles aim to instill a passion for innovative technologies in others by providing practical advice and using an engaging writing style.