Atlantic.Net Blog

How To Run MongoDB as a Docker Container

Hitesh Jethva
by Atlantic.Net (468 posts) under Dedicated Server Hosting
0 Comments

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.

Prerequisites

  • A server running Rocky Linux 8 on the Atlantic.Net Cloud Platform
  • A root password is configured on your server

Step 1 – Create Atlantic.Net Cloud Server

First, log in to your Atlantic.Net Cloud Server. Create a new server, choosing Rocky Linux 8 as the operating system with at least 2GB RAM. Connect to your Cloud Server via SSH and log in using the credentials highlighted at the top of the page.

Once you are logged in to your server, run the following command to update your base system with the latest available packages.

dnf update -y

Step 2 – 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 3 – 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 4 – 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 5 – 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:

[email protected]:/#

Now, run the following command to launch the MongoDB:

[email protected]:/# 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
[email protected]:/# 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 Free To Use Cloud VPS

Free Tier Includes:
G3.2GB Cloud VPS 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


Looking for a Hosting Solution?

We Provide Cloud, Dedicated, & Colocation.

  • Seven Global Data Center Locations.
  • Flexible Private, Public, & Hybrid Hosting.
  • 24x7x365 Security, Support, & Monitoring.
Contact Us Now! Med Tech Award FTC
SOC Audit HIPAA Audit HITECH Audit

Recent Posts

Best Cloud Consultancy or MSP in 2022
BAA Red Flags: What Should Your HIPAA-Compliant Hosting Company Be Willing to Accommodate?
HIPAA Compliance for Remote Workers: How to Maintain HIPAA Compliance with a Remote Team
How to Make the Best Use of the Cloud in the Internet of Things (IoT)
How to Make the Best Use of Artificial Intelligence in Cloud Computing

Get started with 12 months of free cloud VPS hosting

Free Tier includes:
G3.2GB Cloud VPS Server 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


New York, NY

100 Delawanna Ave, Suite 1

Clifton, NJ 07014

United States

San Francisco, CA

2820 Northwestern Pkwy,

Santa Clara, CA 95051

United States

Dallas, TX

2008 Lookout Dr,

Garland, Texas 75044

United States

Ashburn, VA

1807 Michael Faraday Ct,

Reston, VA 20190

United States

Orlando, FL

440 W Kennedy Blvd, Suite 3

Orlando, FL 32810

United States

Toronto, Canada

20 Pullman Ct, Scarborough,

Ontario M1X 1E4

Canada

London, UK

14 Liverpool Road, Slough,

Berkshire SL1 4QZ

United Kingdom

Resources