DEV Community

Cover image for MongoDB with Docker: Get started in 5 minutes
Erin Schaffer for Educative

Posted on • Originally published at educative.io

MongoDB with Docker: Get started in 5 minutes

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:

Why use MongoDB with Docker?

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.

Getting started

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Tip: Check out the Docker Hub to find which tag will work for you.

Setting up a MongoDB container

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Note: tutorial is what we named our MongoDB container.

Interacting with a MongoDB container

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Let’s create a new database and name it “educativeblog”. We can do it with the following command:

use educativeblog
Enter fullscreen mode Exit fullscreen mode

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” })
Enter fullscreen mode Exit fullscreen mode

If we want to query for our MongoDB data, we can do something like this:

db.dogs.find({ name: “Spot” })
Enter fullscreen mode Exit fullscreen mode

Wrapping up and next steps

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:

  • Using MongoDB with Node.js
  • MongoDB server
  • MongoDB API and JSON entities

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!

Continue learning about MongoDB and Docker

Top comments (1)

Collapse
 
javitolin profile image
Javi

Running this way will only lead to missery and regretful thoughts as all the data will be lost when restarting the container.
You should add, at least in a comment, how to setup a volume to save the data in the host.