Running MongoDB in a Docker Container in 5 Minutes
Published: 2022-11-11 11:15:51

If you're looking out for a scalable document database, [MongoDB](https://www.mongodb.com/) might be the perfect choice for you. It is an open-source, non-relational database management system that uses flexible documents instead of tables and rows to process and store various forms of data. According to the "DB-Engines Ranking - Trend of Document Stores Popularity" chart, Mongo is the #1 popular document database.


![Mongo](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jrnmtwo61jjvgyytc3jz.png)


## Why is Mongo so damn popular?


[MongoDB Docker image](https://hub.docker.com/_/mongo) has been downloaded more than 1 Billion times from Docker Hub. What's driving this significant growth? With its flexible schema approach, it's popular with development teams using agile methodologies.  

![Image2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qyvt5478qm93ucnypvdt.png)


MongoDB is a document database used to build highly available and scalable internet applications. It's easy to learn and get started. More than any other NoSQL database, and dramatically more than any relational database, MongoDB's document-oriented data model makes it exceptionally easy to add or change fields, among other things. So if a developer needs to quickly evolve an application, MongoDB's flexible data model facilitates this. Rather than fitting an application to meet schema requirements, the developer writes her application and the schema follows.

## Getting Started

## Prerequisite

In order to run the following instructions, you will need to install Docker Desktop. You can find the installation instructions for your operating system on the [Docker website](https://www.docker.com/products/docker-desktop/)

## Running MongoDB container for the first time

```
docker run --name mongo -d -p 27017:27017 mongo 
```

This command will start a MongoDB server running the latest available version in detached mode (as a background process). You can also see the container running on Docker Desktop Dashboard as shown: 



![Image2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sxap55n4y32rekxsiae7.png)


## Viewing the container details


![Image4](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lvobfyd2dykwlyrk2gxb.png)


## Viewing the stats


![Image5](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1exc7qr5cjf35e0jbt4i.png)


## Data Persistence

Any data created as part of the lifecycle of that container will be destroyed once the container is deleted. If you want to persist the data on your local machine, you can mount a volume using the -v argument.

First, let us create a data directory on a suitable volume on your host system, e.g. /data

```
docker run --name mongo-data -p 27018:27017 -v /data:/data/db -d mongo
```

If you try to run the above command, it will throw error. This is expected.

```
 docker run --name mongo-db -p 27018:27017 -v /data:/data/db -d mongo 
```

## Results:

```
f0737f4ac0722ebf1779bc037ad492baef93906b1859deb8a6a81c32cfd0ae10
docker: Error response from daemon: Mounts denied: 
The path /data is not shared from the host and is not known to Docker.
You can configure shared paths from Docker -> Preferences... -> Resources -> File Sharing.
See https://docs.docker.com/desktop/mac for more info.
```

Reason: You need to enable File sharing for this new directory /data. Let's do it now.

Open Docker Desktop > Preference > Resources > File Sharing


![Image6](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ixuqjck2alxobc8c6304.png)


```
docker run --name mongo-db -p 27018:27017 -v /Users/ajeetraina/data:/data/db -d mongo
```

## Result:


```
501b2cad18ec74338b814deaedc349acc58e7b8e408707c98fe51f69f950546c
```

Hence, we have 2 instance of Mongo running on two different ports - 27017 and 271018 with persistence.

```
 docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS          PORTS                      NAMES
501b2cad18ec   mongo     "docker-entrypoint.s…"   40 seconds ago   Up 39 seconds   0.0.0.0:27018->27017/tcp   mongo-db
2874aa4e3041   mongo     "docker-entrypoint.s…"   17 minutes ago   Up 17 minutes   0.0.0.0:27017->27017/tcp   mongo
```


![Image8](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pedgung90r1a7c3fj20u.png)


## Viewing the mounted volume under Docker Dashboard

![Image9](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9wv6pv3xpn1h7axbl9yk.png)

Also, if you try to see what's inside the /data directory, it dumps default number of files and directories inside it.

```
data % ls
WiredTiger				diagnostic.data
WiredTiger.lock				index-1--5162014189249737448.wt
WiredTiger.turtle			index-3--5162014189249737448.wt
WiredTiger.wt				index-5--5162014189249737448.wt
WiredTigerHS.wt				index-6--5162014189249737448.wt
_mdb_catalog.wt				journal
collection-0--5162014189249737448.wt	mongod.lock
collection-2--5162014189249737448.wt	sizeStorer.wt
collection-4--5162014189249737448.wt	storage.bson
%
```

## Building a To-do List app using React application with a NodeJS backend and a MongoDB database

Let us try to deploy a todo list app using Docker Awesome-compose repository.

## Clone the repository

```
git clone https://github.com/docker/awesome-compose
cd react-express-mongodb
```
## Running the Compose services

```
docker compose up -d
```


![image9](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/41tw1fccaccm9rvwby9r.png)


## Accessing the app

![Image111](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0o0alyhpth18cetm7l13.png)


## Running Mongo Express Docker Extension

Recently I built [Mongo Express Extension for Docker](https://github.com/collabnix/mongoexpress-docker-extension). Mongo Express is a web-based MongoDB admin interface written with Node.js, Express and Bootstrap3. With Mongo Express Docker Extension, now you can setup Mongo Express along with MongoDB with few clicks.


## Getting Started

Ensure that Docker Extensions is enabled in Docker Dashboard.


![Image11](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/txg8ypjq07hvm3hsqt1y.png)




```
git clone https://github.com/collabnix/mongoexpress-docker-extension
```


## Build the Extension

```
make build-extension
```

## Install the Extension

```
docker extension install ajeetraina/mongodb-express-docker-extension:1.0
```

Wait for the above command to finish.  Go to Docker Extension section in the left sidebar to access Mongo Express Docker Extension.

![Image13](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wtbqzdmc9isgzumzug04.png)

## Accessing the document

![Image14](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t9qcebt13mobfhhfhejf.png)


## References:

- [Mongo Express Docker Hub](https://hub.docker.com/_/mongo-express)
- [React + NodeJS + Mongo](https://github.com/docker/awesome-compose/tree/master/react-express-mongodb)

## Want to contribute?

Collabnix community is maintaining a curated list of [Docker community Extension](https://github.com/collabnix/docker-community-extensions) that you might find useful. If you are working on a new Docker Extension, feel free to raise PR and get it added to the list.