Running MongoDB on Docker Compose

abhishek Jain

New Member
In this article, we will discuss how DBA can run a MongoDB instance using docker-compose. It’s very easy and quite flexible to handle. According to my opinion docker-compose removes all the installation and configuration pain when you need a test instance immediately. In a non-production environment for proof of concepts (POC) environment, you can easily use MongoDB on docker-compose.

High-Level Steps for Installation & Configuration
  • Install Docker
  • Install Docker compose
  • Take the docker-compose code with MongoDB
  • Run the docker-compose
  • Connect to MongoDB Database
  • Connect From MongoDB from Docker bash

mkdir -p /opt/docker_com_repo
cd /opt/docker_com_repo
vi docker-compose.yml
Copy Below docker compose code for MongoDB and paste in side the docker-compose.yml

IMP: Remove all the comments with “< abc>” From compose code
mkdir -p /opt/mongo/datafiles/db
mkdir -p /opt/mongo/configfiles

Docker Compose Code
version: ‘3.3’
container_name: mongodb4.0
image: mongo:4.0 < Container Image>
MONGO_INITDB_ROOT_USERNAME: root < Database Admin username>
MONGO_INITDB_ROOT_PASSWORD: oracle < Database Admin Password>
– 27017:27017
– /opt/mongo/datafiles/db:/data < Persistent Volume for Data files>
– /opt/mongo/configfiles:/etc/mongod < Persistent volume for MongoDB configuration file>

Ruing Docker Compose

cd /opt/docker_com_repo
docker-compose up -d
Check if the MongoDB instance started?
[[email protected] mongodb]# docker-compose ps
Name Command State Ports
———————————————————————————————– mongod Up>27017/tcp,:::27017->27017/tcp

Test Database connection
[[email protected] mongodb]# telnet localhost 27017
Trying ::1…
Connected to localhost.
Escape character is ‘^]’.

if looks good than go for the next step or stop the Linux firewall

Open MongoDB compass and connect to Database. Follow the screenshots

Click on “Fill in connection Fields individually



Super Moderator
Staff member
Gold Supporter
We usually ask people to contribute to the entire forum for a while before posting links to sites they're affiliated with. We're pretty lenient, but you have to contribute to the site in general if you want to share your links here. Sharing links to your site should be secondary to your other forum activities. It's allowed, but ya gotta participate more fully.
$100 Digital Ocean Credit
Get a free VM to test out Linux!

Members online