DEV Community

Cover image for Running MongoDB in a Docker Container in 5 Minutes
Ajeet Singh Raina for Docker

Posted on • Updated on

Running MongoDB in a Docker Container in 5 Minutes

If you're looking out for a scalable document database, MongoDB 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

Why is Mongo so damn popular?

MongoDB Docker image 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

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

Running MongoDB container for the first time

docker run --name mongo -d -p 27017:27017 mongo 
Enter fullscreen mode Exit fullscreen mode

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

Viewing the container details

Image4

Viewing the stats

Image5

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

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

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

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

docker run --name mongo-db -p 27018:27017 -v /Users/ajeetraina/data:/data/db -d mongo
Enter fullscreen mode Exit fullscreen mode

Result:

501b2cad18ec74338b814deaedc349acc58e7b8e408707c98fe51f69f950546c
Enter fullscreen mode Exit fullscreen mode

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

Image8

Viewing the mounted volume under Docker Dashboard

Image9

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

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

Running the Compose services

docker compose up -d
Enter fullscreen mode Exit fullscreen mode

image9

Accessing the app

Image111

Running Mongo Express Docker Extension

Recently I built Mongo Express Extension for Docker. 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

git clone https://github.com/collabnix/mongoexpress-docker-extension
Enter fullscreen mode Exit fullscreen mode

Build the Extension

make build-extension
Enter fullscreen mode Exit fullscreen mode

Install the Extension

docker extension install ajeetraina/mongodb-express-docker-extension:1.0
Enter fullscreen mode Exit fullscreen mode

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

Image13

Accessing the document

Image14

References:

Want to contribute?

Collabnix community is maintaining a curated list of Docker community Extension 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.

Top comments (0)