DEV Community

Asad
Asad

Posted on

πŸ—ƒοΈ MongoDB & Mongo Express – Docker Setup

This guide provides a quick and easy way to run MongoDB and Mongo Express using Docker. It includes their purpose, Docker usage, and troubleshooting steps for container management.


πŸ“Œ Overview

πŸ”Ή MongoDB

MongoDB is a popular NoSQL database that stores data in flexible, JSON-like documents. It’s ideal for scalable, high-performance applications where schema flexibility is important.

πŸ”Ή Mongo Express

Mongo Express is a lightweight, web-based admin interface for MongoDB. It allows you to:

  • View and manage databases
  • Browse collections and documents
  • Execute queries and CRUD operations
  • Use a user-friendly web UI to interact with MongoDB

πŸš€ Getting Started

Ensure Docker is installed and a custom Docker network exists:

docker network create project-network
Enter fullscreen mode Exit fullscreen mode

🐳 Start MongoDB Container

docker run -d --name mongodb \
  --network project-network \
  -e MONGO_INITDB_ROOT_USERNAME=admin \
  -e MONGO_INITDB_ROOT_PASSWORD=password \
  -v mongo-data:/data/db \
  mongo:4.4
Enter fullscreen mode Exit fullscreen mode

Explanation:

  • Uses mongo:4.4 (recommended for CPUs without AVX support)
  • Stores data in a named volume: mongo-data
  • Runs on a shared Docker network for Mongo Express to connect

🐳 Start Mongo Express Container

docker run -d --name mongo-express \
  --network project-network \
  -e ME_CONFIG_MONGODB_ADMINUSERNAME=admin \
  -e ME_CONFIG_MONGODB_ADMINPASSWORD=password \
  -e ME_CONFIG_MONGODB_SERVER=mongodb \
  -e ME_CONFIG_BASICAUTH_USERNAME=admin \
  -e ME_CONFIG_BASICAUTH_PASSWORD=pass123 \
  -p 8083:8081 \
  mongo-express:latest
Enter fullscreen mode Exit fullscreen mode

Access the Web UI: http://localhost:8083

Login Credentials:

  • Username: admin
  • Password: pass123

πŸ›‘ Troubleshooting: Force Stop Unresponsive Containers

If docker stop fails, you can manually stop the container using the container’s process ID:

1. Get the container's PID

docker inspect --format '{{.State.Pid}}' <container_name>
Enter fullscreen mode Exit fullscreen mode

2. Kill the process

sudo kill -9 <PID>
Enter fullscreen mode Exit fullscreen mode

3. Remove the container

docker rm <container_name>
Enter fullscreen mode Exit fullscreen mode

(Optional) Restart Docker daemon

sudo systemctl restart docker
Enter fullscreen mode Exit fullscreen mode

standard Docker commands to stop containers and clean up your environment:


πŸ›‘ Stop Docker Containers

Stop a specific container:

docker stop <container_name_or_id>
Enter fullscreen mode Exit fullscreen mode

Stop all running containers:

docker stop $(docker ps -q)
Enter fullscreen mode Exit fullscreen mode

🧼 Remove Docker Containers

Remove a specific container:

docker rm <container_name_or_id>
Enter fullscreen mode Exit fullscreen mode

Remove all stopped containers:

docker container prune
Enter fullscreen mode Exit fullscreen mode

Remove all containers (running or stopped):

docker rm -f $(docker ps -aq)
Enter fullscreen mode Exit fullscreen mode

πŸ—‘οΈ Remove Docker Images

Remove a specific image:

docker rmi <image_id>
Enter fullscreen mode Exit fullscreen mode

Remove all unused images:

docker image prune
Enter fullscreen mode Exit fullscreen mode

Remove all images:

docker rmi -f $(docker images -aq)
Enter fullscreen mode Exit fullscreen mode

🧹 Remove Docker Volumes (Optional)

Remove unused volumes:

docker volume prune
Enter fullscreen mode Exit fullscreen mode

Remove all volumes:

docker volume rm $(docker volume ls -q)
Enter fullscreen mode Exit fullscreen mode

πŸ”„ Restart Docker (if needed)

sudo systemctl restart docker
Enter fullscreen mode Exit fullscreen mode

🧹 Cleanup Docker Resources (Extreme Measures)

To remove all containers and images:

docker stop $(docker ps -aq)
docker rm $(docker ps -aq)
docker rmi -f $(docker images -aq)
Enter fullscreen mode Exit fullscreen mode

Top comments (0)