Atlantic.Net Blog

How To Run MongoDB as a Docker Container

MongoDB is a free, open-source, and NoSQL database system. It is designed for high availability and scalability and is ideal for managing dynamic workloads. Deploying MongoDB as a Docker container allows us to have a portable database that can run on any platform and operating system. You don’t need to worry about its configuration. It will provide consistency and speed up the development process.

In this post, we will show you how to run MongoDB as a Docker Container.

dnf update -y

Step 1 – Install Docker CE

Before starting, Docker CE must be installed on your server. If not installed, you can install it by following the below steps.

First, add the Docker CE repo with the following command:

dnf config-manager --add-repo=https://download.docker.com/linux/centos/docker-ce.repo

Once the Docker CE repo is created, run the following command to start the installation:

dnf install docker-ce -y

Once the Docker CE is installed, start the Docker service and enable it to start at system reboot:

systemctl start docker
systemctl enable docker

Also Read

How to Install and Use MongoDB on Rocky Linux 8

Step 2 – Download MongoDB Docker Image

First, you will need to download the MongoDB image from the Docker registry. To download the latest stable version of MongoDB image, run the following command:

docker pull mongo

To download a specific version of MongoDB, run the following command:

docker pull mongo:4.2.2

You will get the following output:

4.2.2: Pulling from library/mongo
5c939e3a4d10: Pull complete 
c63719cdbe7a: Pull complete 
19a861ea6baf: Pull complete 
651c9d2d6c4f: Pull complete 
85155c6d5fac: Pull complete 
85fb0780fd97: Pull complete 
85b3b1a901f5: Pull complete 
6a882e007bb6: Pull complete 
f7806503a70f: Pull complete 
e23d5068c270: Pull complete 
56eb708963d7: Pull complete 
fc4def32f081: Pull complete 
ea1dc19faea9: Pull complete 
Digest: sha256:e6d9a22056418759bb38f96996756360a57cce49c1d29ba663ebb413abdaa48f
Status: Downloaded newer image for mongo:4.2.2
docker.io/library/mongo:4.2.2

Next, verify the downloaded image using the following command:

docker images

You will get the following output:

REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
mongo        4.2.2     105a8b77784b   2 years ago   364MB

Step 3 – Launch MongoDB Container

At this point, the MongoDB docker image is downloaded. Now, you will need to create a container from the downloaded image.

Run the following command to start a MongoDB container:

docker run -it --name mongodb-container -d mongo:4.2.2

Once the MongoDB container starts, you can check it with the following command:

docker ps

You should see that the MongoDB container is started and listening on port 27017

CONTAINER ID   IMAGE         COMMAND                  CREATED         STATUS         PORTS       NAMES
222837c70ee3   mongo:4.2.2   "docker-entrypoint.s…"   3 seconds ago   Up 3 seconds   27017/tcp   mongodb-container

You can also check the MongoDB container log using the following command:

docker logs mongodb-container

You will get the following output:

2022-02-01T07:05:53.414+0000 I  STORAGE  [LogicalSessionCacheRefresh] createCollection: config.system.sessions with provided UUID: e7463f47-cccd-4b18-8f42-61b8e824d906 and options: { uuid: UUID("e7463f47-cccd-4b18-8f42-61b8e824d906") }
2022-02-01T07:05:53.414+0000 I  NETWORK  [initandlisten] Listening on /tmp/mongodb-27017.sock
2022-02-01T07:05:53.414+0000 I  NETWORK  [initandlisten] Listening on 0.0.0.0
2022-02-01T07:05:53.414+0000 I  NETWORK  [initandlisten] waiting for connections on port 27017
2022-02-01T07:05:53.420+0000 I  INDEX    [LogicalSessionCacheRefresh] index build: done building index _id_ on ns config.system.sessions
2022-02-01T07:05:53.425+0000 I  INDEX    [LogicalSessionCacheRefresh] index build: starting on config.system.sessions properties: { v: 2, key: { lastUse: 1 }, name: "lsidTTLIndex", ns: "config.system.sessions", expireAfterSeconds: 1800 } using method: Hybrid
2022-02-01T07:05:53.425+0000 I  INDEX    [LogicalSessionCacheRefresh] build may temporarily use up to 500 megabytes of RAM
2022-02-01T07:05:53.425+0000 I  INDEX    [LogicalSessionCacheRefresh] index build: collection scan done. scanned 0 total records in 0 seconds
2022-02-01T07:05:53.426+0000 I  INDEX    [LogicalSessionCacheRefresh] index build: inserted 0 keys from external sorter into index in 0 seconds
2022-02-01T07:05:53.427+0000 I  INDEX    [LogicalSessionCacheRefresh] index build: done building index lsidTTLIndex on ns config.system.sessions
2022-02-01T07:05:53.428+0000 I  SHARDING [LogicalSessionCacheReap] Marking collection config.transactions as collection version: 
2022-02-01T07:05:54.000+0000 I  SHARDING [ftdc] Marking collection local.oplog.rs as collection version: 

Also Read

How to Deploy WordPress with Docker and Docker Compose

Step 4 – Connect MongoDB Container

At this point, the MongoDB container is started and running in the detached mode. If you want to connect to the MongoDB container, run the following command:

docker exec -it mongodb-container bash

Once you are connected, you will get the following shell:

root@222837c70ee3:/#

Now, run the following command to launch the MongoDB:

root@222837c70ee3:/# mongo

You will get the following output:

Enable MongoDB's free cloud-based monitoring service, which will then receive and display
metrics about your deployment (disk utilization, CPU, operation statistics, etc).

The monitoring data will be available on a MongoDB website with a unique URL accessible to you
and anyone you share the URL with. MongoDB may use this information to make product
improvements and to suggest MongoDB products and deployment options to you.

To enable free monitoring, run the following command: db.enableFreeMonitoring()
To permanently disable this reminder, run the following command: db.disableFreeMonitoring()
---

> 

Now, exit from the MongoDB shell and container using the following command:

> exit
root@222837c70ee3:/# exit

Conclusion

In the above guide, we explained how to run MongoDB as a Docker container. We also explained how to access the MongoDB container to manage the MongoDB databases. Try deploying MongoDB on bare metal servers from Atlantic.Net!

Get a $250 Credit and Access to Our Free Tier!

Free Tier includes:
G3.2GB Cloud VPS a Free to Use for One Year
50 GB of Block Storage Free to Use for One Year
50 GB of Snapshots Free to Use for One Year