MongoDB is a popular NoSQL database that uses documents for data storage. MongoDB is considered schema-less, which means that it doesnāt require a defined database schema. Itās a great tool if you want to scale and evolve quickly, as it supports rapid iterative development and allows multiple team members to collaborate.
Docker is a tool that you can use to build applications that run on your host operating system. It runs natively on Linux. Docker uses containers and allows you to combine your application with all its dependencies into a single unit. With Docker, itās easy to create a container and start working with different technologies.
In this tutorial, weāll create a MongoDB container using Docker.
Letās get started!
Weāll cover:
Get hands-on with MongoDB
Learn MongoDB fast with interactive exercises.
MongoDB enables high availability and scalability. It works well in distributed environments like Docker containers. Using MongoDB with Docker allows us to have a portable database that can be run on any server platform without having to worry about its configuration. We can use Docker with a MongoDB container image to make the database deployment process more efficient and straightforward.
Containerizing a database provides consistency across different environments and enables a faster development setup. Docker containers are extremely portable, meaning that you can use containers wherever you want to operate. They run easily on the cloud and work with multiple cloud platforms.
Itās time to create our Mongo container! Before we can begin, letās make sure we have Docker installed onto our device. Once we have Docker installed, weāre ready to get started.
Weāll begin by downloading the latest MongoDB Docker image from Docker Hub. A Docker image is a file that allows us to execute code in a Docker container. This file holds instructions for creating containers that run on Docker. There are different versions of MongoDB images. Each Mongo image serves a different purpose.
Weāll use the standard Mongo image and use the following command:
sudo docker pull mongo
If we want to download a specific version of MongoDB, we can use the same command and append the version tag. It would look something like this:
sudo docker pull mongo:4.0.4
Tip: Check out the Docker Hub to find which tag will work for you.
Now that weāve created our Mongo image, weāre ready to set up our MongoDB container. Weāll use a Docker run command to deploy a MongoDB instance and also give it a container name:
docker run --name tutorial mongo
If we downloaded the 4.0.4 version of Mongo, weād append the version tag to the end like this:
docker run --name tutorial mongo:4.0.4
Note:
tutorial
is what we named our MongoDB container.
Learn MongoDB without scrubbing through videos or documentation. Educativeās text-based courses are easy to skim and feature live coding environments, making learning quick and efficient.
We created our container! Weāre going to interact with the database through the bash shell client. Weāll use the docker exec
command in the interactive terminal to connect to it:
docker exec -it tutorial bash
The above command will connect to our deployment named tutorial
using the interactive terminal. Itāll also start the bash shell. Now, weāre ready to start using MongoDB. We can use the following command to launch the MongoDB shell client:
mongo
Letās create a new database and name it āeducativeblogā. We can do it with the following command:
use educativeblog
We canāt use our database until we add data to it. Weāre going to create three documents in a dogs
collection that will exist in our educativeblog
database.
db.dogs.save({ name: āSpotā })db.dogs.save({ name: āLuckyā })db.dogs.save({ name: āMochiā })
If we want to query for our MongoDB data, we can do something like this:
db.dogs.find({ name: āSpotā })
Congrats on taking your first steps with MongoDB and Docker! Running containers with Docker is very efficient. Creating a MongoDB container allows us to work with a portable and extensible NoSQL database without worrying about the underlying configuration of the devices we want to run it on. MongoDB is the most popular NoSQL database system, and it can be used for many things. Thereās still so much more to learn about MongoDB. Some recommended topics to cover next include:
To get started learning these concepts and more, check out Educativeās course The Definitive Guide to MongoDB. In this interactive course, youāll get to see for yourself why thereās so much hype around MongoDB. Youāll learn basic Mongo command operations, using MongoDB in C# and .NET Core, and much more. Throughout the way, youāll use MongoDB to build projects as you learn. By the end, youāll know all about how to use MongoDB databases.
Happy learning!
Free Resources