MongoDB with Docker: Get started in 5 minutes
Tags: mongodb, docker, database, datascience
Published: 2021-07-01 22:01:30

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?](#whyuse)
* [Getting started](#gettingstarted)
* [Setting up a MongoDB container](#settingup)
* [Interacting with a MongoDB container](#interacting)
* [Wrapping up and next steps](#nextsteps)
<br>

<a name="whyuse"></a>

## 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](https://www.educative.io/blog/beginners-guide-to-docker) 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. 
<br>

<a name="gettingstarted"></a>

## 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
```

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.

<br>

<a name="settingup"></a>

## 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
```

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.

<br>

<a name="interacting"></a>

## 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
```

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” })
```

<br>

<a name="nextsteps"></a>

## 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**](https://www.educative.io/courses/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!*
 
<br>

### Continue learning about MongoDB and Docker

* [Getting Started with Docker and Kubernetes: a beginners guide](https://www.educative.io/blog/docker-kubernetes-beginners-guide)
* [Docker Compose Tutorial: advanced Docker made simple](https://www.educative.io/blog/docker-compose-tutorial)
* [MongoDB vs PostgreSQL: what to consider when choosing a database](https://www.educative.io/blog/mongodb-versus-postgresql-databases)