Edited 2 weeks ago by ExtremeHow Editorial Team
DockerLinuxMongoDBDatabaseContainerizationDevelopmentVirtualizationDeploymentSetupConfiguration
This content is available in 7 different language
MongoDB is a popular NoSQL database that allows flexible, scalable data management. Docker, on the other hand, is a platform that allows software to run in containers, ensuring that applications can run consistently on any platform. Using Docker to set up MongoDB on Linux combines the flexibility of MongoDB with Docker's powerful containerization capabilities. This guide will introduce you to the process of setting up MongoDB on Docker in a Linux environment. We will cover every step in detail to ensure that you can implement it successfully.
Before you begin setting up MongoDB on Docker, make sure you have the following:
If Docker is not installed on your Linux system, you can follow these steps to install it:
sudo apt update
sudo apt install apt-transport-https ca-certificates curl gnupg-agent software-properties-common
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
sudo apt update
sudo apt install docker-ce docker-ce-cli containerd.io
After following these steps, Docker should be successfully installed on your Linux system. You can check the installed version using the following:
docker --version
Now that Docker is installed, the next step is to pull the MongoDB image from Docker Hub. Docker Hub is a cloud-based library where users can find images for various software. To pull the MongoDB image, use the following command:
sudo docker pull mongo
This command will download the latest version of MongoDB from Docker Hub. You can specify a particular version by adding a colon (:) and the version number after "mongo". For example, to pull version 4.4, you would use:
sudo docker pull mongo:4.4
After the MongoDB image is downloaded, you can create and run a container using the image. A container is like a lightweight virtual machine that can run different applications. Execute the following command to run a MongoDB container:
sudo docker run --name mongodb -d -p 27017:27017 mongo
Analysis of the order:
--name mongodb
: Assigns the name "mongodb" to the container.-d
: Runs the container in isolated mode, i.e. it will run in the background.-p 27017:27017
: Maps port 27017 of the host machine to port 27017 of the container, which is the default port on which MongoDB listens.mongo
: Specifies the MongoDB image to use.Once the MongoDB container is running, you must verify that everything is set up correctly. Check the status of the Docker container using the following:
sudo docker ps
This will display the list of running containers, and you will also see the MongoDB container among them. It will show details like container ID, name, image used, and mapped port.
You can further verify this by accessing MongoDB using the MongoDB shell. First, log into the running MongoDB container:
sudo docker exec -it mongodb bash
Inside the container shell, start the MongoDB shell by typing the following:
mongo
This will give you a MongoDB shell prompt, which will confirm that MongoDB is running inside the Docker container.
By default, any data stored in a Docker container is non-persistent. This means that if the container is deleted, all data created within it will be lost. To persist the data, you must map a directory on the host machine to a directory inside the container.
To do this, stop the MongoDB container:
sudo docker stop mongodb
Then run a new container with the volume mapping:
sudo docker run --name mongodb -d -p 27017:27017 -v /my/own/datadir:/data/db mongo
Explanation of the new flag:
-v /my/own/datadir:/data/db
: Maps the /data/db
directory inside the container to /my/own/datadir
on the host machine.Replace /my/own/datadir
with the path where you want to save MongoDB data on your host machine. This directory will store all MongoDB data, and it will persist even if you delete the container.
Running MongoDB with default settings may not be safe for a production environment. Here are some basic steps to enhance security.
First, start the MongoDB shell and create an admin user:
mongo
Then go to the admin
database:
use admin
Create an admin user by executing the following command. Make sure to replace "username" and "password" with your desired username and password:
db.createUser({ user: "username", pwd: "password", roles: [ { role: "userAdminAnyDatabase", db: "admin" } ] })
To turn on authentication, you need to edit the configuration file. Exit the shell and stop the running container:
exit
sudo docker stop mongodb
Start a new MongoDB container with "auth" enabled:
sudo docker run --name mongodb -d -p 27017:27017 -v /my/own/datadir:/data/db mongo --auth
Now, whenever you connect to the database, it will require authentication.
For more advanced configuration, you can provide a configuration file when starting the MongoDB container. Create the configuration file on the host machine and pass it to the container using the following command:
sudo docker run --name mongodb -d -p 27017:27017 -v /my/own/datadir:/data/db -v /my/own/config:/etc/mongo mongo --config /etc/mongo/mongod.conf
Replace /my/own/config
with the directory where your mongod.conf
file is located. This allows you to customize settings such as replica sets, sharding, and logging.
Setting up MongoDB on Docker for Linux provides a flexible and efficient way to manage the database, with the added benefit of Docker's containerization. This ensures consistency across environments and simplifies deployment processes. By following these steps, you will be able to effectively run and secure MongoDB on Docker.
Docker's isolation features allow you to easily create and delete containers, making testing and management much simpler. Adding security such as data persistence and authentication allows MongoDB's robust features to be safely leveraged in a production environment. Remember that with containerization, knowing how to handle data storage and configuration can greatly increase the performance and reliability of your database setup.
If you find anything wrong with the article content, you can