Setup MongoDB with Docker and Docker-Compose step-by-step

Setup MongoDB with Docker and Docker-Compose step-by-step

MongoDB is an open-source document database management system designed to provide high availability. In this article, you will learn how run MongoDB with Docker and Docker-Compose. Let's do it...

What is MongoDB ?

MongoDB is a popular open-source NoSQL database offering high scalability and flexibility for large volumes of data. MongoDB stores data in files using a format called BSON (Binary JSON) and features an expressive query language, helping developers to work with data naturally as you can store and retrieve JSON easily.

MongoDB also has a cloud offering called MongoDB Atlas.

What is Docker ?

Docker is a platform used to create, develop and run applications in containers. Containers are lightweight, portable and operable packages that contain everything needed to run an application, such as code, runtime, libraries and dependencies.

Docker enables developers to build applications in a consistent environment, package them in containers, and deploy them across different environments, including development, test, and production.

Run MongoDB with Docker

In this article we are going to use the latest version of MongoDB on March 2024 : mongo:7.0.7. To Run MongoDB 7 with docker you can execute the Docker run command which will pull the Docker image if it's not available locally and run it.

docker run --rm --name mongo7-test -e MONGO_INITDB_ROOT_USERNAME=admin -e MONGO_INITDB_ROOT_PASSWORD=VRuAd2Nvmp4ELHh5 -e MONGO_INITDB_DATABASE=test -v /data/mongo:/data/db mongo:7.0.7

docker run command is used to create an new container and start the container.
--rm is used to automatically delete the container when it stops running.
--name is followed by the name we want to assign to the container for reference.
-e is followed by environment variables MONGO_INITDB_ROOT_USERNAME or MONGO_INITDB_ROOT_PASSWORD or MONGO_INITDB_DATABASE which get respectively the username of the root database, the password and the name of database name
-v /data/mongo:/data/db mounts a volume, mapping the '/data/mongo' directory on your host machine to the '/data/db' directory within the container. This ensures that your database data persists even if the container is deleted.
mongo:7.0.7 specifies the Docker image to use, in this case, the official MongoDB image tagged with version 7.0.7.

After the container creates and running with the name mongo7-test we can notify in logs waiting for connections on port 27017 as seen in this image

Now we can execute other command inside our running container using mongo shell .

docker exec -it mongo7-test mongosh -u admin -p VRuAd2Nvmp4ELHh5 --authenticationDatabase admin

docker exec command is used to execute a running container
-iti option used to allowing interaction inside the container & t is used to open a session in a regular terminal
mongo7-test is the name of the running container we want to execute
mongosh is the MongoDB shell we are going to use to interact with the database
-u followed by "admin" to specify the username for authentication
-p followed by "VRuAd2Nvmp4ELHh5" to specify the password for authentication
--authenticationDatabase admin indicates that the provided credentials are for the "admin'" database.

Then we have access to our database with mongo shell inside the container. Now we can execute mongosh commands to insert, update, delete(...) on our database

Example:

We can see that we insert in a table tutorial a new record, and after that we request to find all records in tutorial table and get a list

Boom!! Now we are able to run MongoDB with Docker and interact with the database.
In the next section, you will learn how to run MongoDB with docker-compose.

Run MongoDB with docker-compose

What is docker-compose

Docker Compose is Docker tool that allows developers to define all the differents containers that make up their application in a single file called docker-compose.yml

Now to run MongoDB with docker compose we need to create our file docker-compose.yml

# docker-compose.yml
version: '3.8'

services:
    mongodb:
        image: mongo:7.0.7
        restart: always
        environment:
            MONGO_INITDB_ROOT_USERNAME: admin
            MONGO_INITDB_ROOT_PASSWORD: VRuAd2Nvmp4ELHh5
        ports:
            - 27017:27017
        volumes:
            - mongo-data:/data/db
volumes:
    mongo-data:

This is a simple docker-compose file with one service mongodb to setup mongo:7.0.7.
version is used to specify the version of docker-compose file format
services list all services we want to set up in our application
mongodb is our first service name so the container will be called mongodb
image is used to specified docker images we will use
restart option is used to restart the container every time you on your computer
environment follow by MONGO_INITDB_ROOT_USERNAME and MONGO_INITDB_ROOT_PASSWORD root username and password for authentication in our database
ports is used to map our computer 27017 port with the port 27017 of our database inside the mongodb container
volumes are used to create a mongo-data volumes to persist the container data even it is removed

Run mongodb with docker-compose

We can run our docker-compose file with the following command

docker compose up -d

-d is used to run the container in background mode

After that we are able to exec the running container mongodb and make some operation inside the container

Conclusion

In this article, we first learned about what is MongoDB, Docker & Docker-compose. Then you learned how to run Mongo DB with Docker run commands. After that we learn how to run mongo:7.0.7 in docker-compose. Also, we discover some options of docker and docker-compose commands, for more details go to docker documentation

I hope you learned how to run MongoDB with Docker and Docker Compose.
See you soon