Setting Up MongoDB with Mongo-Express Using Docker and connecting Test app to mongoDB

Omkar MahanduleOmkar Mahandule
3 min read

In this blog, I'll walk you through setting up MongoDB with Mongo-Express using Docker. Mongo-Express is a lightweight web-based administrative interface for MongoDB, making it easy to visualize and manage your database.

Prerequisites

  • Docker installed on your system

  • Basic understanding of Docker commands

Let's dive in!

Step 1: Node.js Application (Can be done with any application)

  • Build the image of your application, for that use following command:

      docker build -t docker-testapp:1.0 .
    

  • This will creates the image for your application.

  • After that we have to run this newly created image as:

       docker run -d -p 5050:5050 --name docker-testapp-container docker-testapp:1.0
    

Step 2: Create the Docker Compose File

First, create a docker-compose.yml file to define the MongoDB and Mongo-Express services. Here’s an example:

version: '3.8'
services:
  mongo:
    image: mongo
    ports:
      - "27017:27017"
    environment:
      MONGO_INITDB_ROOT_USERNAME: admin
      MONGO_INITDB_ROOT_PASSWORD: qwerty
    volumes:
      - ./mongo-data:/data/db

  mongo-express:
    image: mongo-express
    ports:
      - "8081:8081"
    environment:
      ME_CONFIG_MONGODB_ADMINUSERNAME: admin
      ME_CONFIG_MONGODB_ADMINPASSWORD: qwerty
      ME_CONFIG_MONGODB_URL: mongodb://admin:qwerty@mongo:27017/
    depends_on:
      - mongo

This configuration creates two services:

  • mongo: Runs the latest MongoDB container.

  • mongo-express: Provides a web-based GUI to interact with the MongoDB instance.

Step 3: Run Docker Compose

Navigate to the folder containing docker-compose.yml and run:

docker-compose -d -f docker-compose.yml up

This command pulls the required images and starts the containers in detached mode.

Step 4: Verify the Containers

Check the running containers with:

docker ps

You should see two containers running: mongo and mongo-express.

Step 5: Access Mongo-Express

Open your browser and go to:

http://localhost:8081

You should see the Mongo-Express interface where you can manage your MongoDB databases.

Mongo-Express Home Page:

This is the Mongo-Express home screen showing the available databases.

Step 6: Add a New User

You can add a new user directly from Mongo-Express. Click on your database, then the collection, and use the “Add Document” button.

Adding new user:

Step 7: Fetch Data with Your App

If you’re building a Node.js app (or any other backend), you can connect to MongoDB using the URL:

mongodb://localhost:27017

Once connected, you can perform CRUD operations. Below is an example of fetching user data.

This shows the response from MongoDB when fetching user data.

Verifying using getUser:

We can also verifies whether our test app is successfully connected to the mongodb or not.

This displays that our app is now connected to mongoDB database.

Conclusion

Setting up MongoDB with Mongo-Express using Docker is quick and convenient. With just a few commands, you get a full-fledged database and a GUI to manage it.

Happy coding! 🚀

0
Subscribe to my newsletter

Read articles from Omkar Mahandule directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Omkar Mahandule
Omkar Mahandule

I am an aspiring DevOps & Cloud Engineer with a strong drive to learn, grow, and contribute to the world of automation, cloud computing, and scalable infrastructure. Passionate about automation, cloud computing, and scalable infrastructure. With a strong foundation in Linux, Git/GitHub, CI/CD, Docker, Kubernetes, and AWS, I have already completed the core phases of my DevOps journey and am now advancing into Infrastructure as Code (Terraform, Ansible), Monitoring, and DevSecOps. 💻 What I Bring to the Table: 🐧 Linux & Scripting – Shell scripting, process management, automation. 🌱 Version Control – Git, GitHub/GitHub Actions, branching strategies. ⚡ CI/CD Pipelines – Jenkins, GitHub Actions, CI/CD. 🐳 Containers & Orchestration – Docker, Kubernetes (Minikube, EKS). ☁️ Cloud Computing – AWS (EC2, S3, IAM, RDS, VPC, Load Balancers). 🎯 Next Steps in My DevOps Journey: 🏗️ Mastering Infrastructure as Code (Terraform, Ansible) for automated provisioning. 📊 Learning Monitoring & Observability (Prometheus, Grafana, AWS CloudWatch, ELK Stack). 🔒 Exploring DevSecOps by implementing security scanning and best practices in CI/CD. 🛡️ Deepening my expertise in Kubernetes security (RBAC, Network Policies).