Containers (containers) Docker Mongodb are easy to reply and develop. If the load on the database increases, you can launch other Mongodb containers. Thus, the performance of your database remains stable.
Does Mongodb work in a Docker container?
Yes: Mongodb can actually operate in a Docker container. On Docker Hub, there are two types of Mongodb images: the Community Edition and theEdition Edition. Depending on your needs, choose between these two variants. The Community Edition is free and open source; It is used for non -commercial purposes or for small deployments. The Enterprise edition, on the other hand, offers additional features and support adapted to larger applications or business environments with advanced needs, such as encryption, audit and LDAP integration.
If the predefined MongoDB images on Docker Hub do not fully meet your needs, Docker allows you to create your own Personalized Docker Image Using a Dockerfile. You can choose the version of Mongodb, define certain configuration options (for example authentication methods) and install additional tools or drivers for your environment.
Start Mongodb as a Docker container
Docker is characterized by a light virtualization which promotes portability and consistency in different development and production environments. In the following paragraphs, we explain step by step how to configure the Mongodb containers in Docker. For beginners, we recommend our Docker tutorial: we give a detailed introduction to the container platform.
Step 1: Download the Docker Mongodb image
To create a Mongodb Docker container, we start first by obtaining the corresponding image for the execution of Docker Hub. Open your terminal or your command line and run the following order:
docker pull mongo:latest
bash
If the image already exists locally, Docker will not download new version, unless you explicitly indicate the version you want to use (for example mongo:4.4
).
Step 2: Start the Mongodb Docker container
After successfully downloading the Docker image for Mongodb, you can now start a container based on this image:
docker run --name mongodb-container -d -p 27017:27017 mongo:latest
bash
docker run
: Starts a new Docker container--name mongodb-container
: Give the Docker container the name « Mongodb-Container »-d
: Allows you to start the container in the background (detached mode). Thus, the terminal remains usable during the execution of the container.-p 27017:27017
: Opens the standard Mongodb port (27017) of the container on your host system.mongo:latest
: transmits the instruction to obtain the last image available.
Step 3: Check that the container is running
To make sure the container started well, enter the following command:
The information obtained is useful for obtaining a quick overview of the Mongodb Docker Mongodb containers on your system, their execution time and the ports they use. If you need more details, such as specific containers or specific filtering options, it is recommended to use the command docker ps -a
.
Step 4: Connect to Mongodb in the Docker container
You can now connect to the Mongodb instance in your Docker container. To do this, launch the Mongodb Shell directly in the container:
docker exec -it mongodb-container mongo
bash
docker exec
: executes an order in the container in progress-it
: allows you to interact with the terminal in the containermongodb-container
: designates the name of the Mongodb container set upmongo
: Start the Mongodb Shell
After executing this order, you should see the Shell Mongodb ready to receive orders. You can now enter the usual Mongodb commands to manage or question your database.
Step 5: Perform operations in the Mongodb Shell
You can display all existing databases:
The output contains database names:
admin 0.000GB
local 0.000GB
test 0.000GB
bash
To work with a specific database in the mongodb shell, use the following command:
With this command, you place the database with the name « Mydatabase ». If this database does not exist, it is created automatically as soon as a document is inserted.
You can also consult the documents of a specific collection:
The Shell Mongodb returns all the documents stored in the « USERS » collection.
{ "_id": ObjectId("609823e9f9a5f7f364fc3f90"), "username": "alice", "age": 28 }
{ "_id": ObjectId("609823f2f9a5f7f364fc3f91"), "username": "martin", "age": 32 }
{ "_id": ObjectId("609823f9f9a5f7f364fc3f92"), "username": "charles", "age": 25 }
bson
Each document contains a field _id
unique (specific identifier at Mongodb) and other fields such as username
And age
.
You will find more information on Docker tools as well as a presentation of Mongodb with a comparison with MySQL in our digital guide.
Managed databases
Managed and safe databases
- Flexible solutions, adapted to your needs
- Professional level architecture, managed by experts
- Hosted in Europe, in accordance with the strictest data protection standards