This documentation outlines the process to configure and deploy MongoDB 6 within a Dockerized environment using Docker Compose. The MongoDB service will be set up on an Ubuntu 24.04.1-based system, ensuring proper network and volume management for persistent data storage.
- System Requirements
- Project Structure
- Technologies Used
- PostgreSQL Configuration
- Docker Configuration
- Volume Management
- Network Configuration
- Environment Variables
- Resource Management
- Deployment and Start Instructions
- Troubleshooting
- License
This project requires an Ubuntu 24.04.1 (AMD64) system with Docker and Docker Compose installed. Ensure that your system meets the following prerequisites:
- Operating System: Ubuntu 24.04.1 (AMD64)
- Docker Version: 20.10.x or higher
- Docker Compose Version: 1.27.x or higher
This project uses the following directory structure:
project-root/
βββ docker-compose.yml
βββ .env
βββ README.md
- docker-compose.yml: Contains the Docker Compose configuration for MongoDB and Mongo Express.
- .env: Holds the environment variables for MongoDB and Mongo Express credentials.
- README.md: Documentation file for the setup and usage.
- Docker: Containerization platform to isolate MongoDB and Mongo Express services.
- Docker Compose: Multi-container orchestration tool to define and manage the services.
- MongoDB 6: NoSQL database designed for scalability and high availability.
- Mongo Express: Web-based MongoDB admin interface for managing MongoDB databases.
Though the main database used in this setup is MongoDB, if you need to integrate with PostgreSQL for other services, you can configure it similarly to MongoDB in Docker Compose.
This section describes the Docker configuration for MongoDB and Mongo Express.
version: '3.8'
services:
mongodb:
image: mongo:latest
container_name: mongodb
hostname: mongodb
volumes:
- mongodb-data:/data/db/
- mongodb-log:/var/log/mongodb/
environment:
MONGO_INITDB_ROOT_USERNAME: ${MONGO_INITDB_ROOT_USERNAME}
MONGO_INITDB_ROOT_PASSWORD: ${MONGO_INITDB_ROOT_PASSWORD}
MONGO_DB_BIND_IP: 0.0.0.0
MONGO_DB_CACHE_SIZE_GB: 2
MONGO_DB_LOG_PATH: /var/log/mongodb/mongodb.log
ports:
- "27017:27017"
networks:
mongodb_network:
ipv4_address: 172.19.0.2
command: >
mongod --auth
--bind_ip=${MONGO_DB_BIND_IP}
--wiredTigerCacheSizeGB=${MONGO_DB_CACHE_SIZE_GB}
--logpath=${MONGO_DB_LOG_PATH}
--logappend
restart: always
mongo-express:
image: mongo-express:latest
container_name: mongo-express
restart: always
environment:
ME_CONFIG_MONGODB_ADMINUSERNAME: ${MONGO_INITDB_ROOT_USERNAME}
ME_CONFIG_MONGODB_ADMINPASSWORD: ${MONGO_INITDB_ROOT_PASSWORD}
ME_CONFIG_MONGODB_PORT: 27017
ME_CONFIG_MONGODB_SERVER: 'mongodb'
ME_CONFIG_BASICAUTH_USERNAME: ${MONGO_EXPRESS_USERNAME}
ME_CONFIG_BASICAUTH_PASSWORD: ${MONGO_EXPRESS_PASSWORD}
ports:
- "8081:8081"
networks:
mongodb_network:
ipv4_address: 172.19.0.3
depends_on:
- mongodb
volumes:
mongodb-data:
driver: local
name: mongo-data
mongodb-log:
driver: local
name: mongo-log
networks:
mongodb_network:
driver: bridge
ipam:
config:
- subnet: "172.19.0.0/16"
Volumes are defined for MongoDB data and logs to ensure persistence:
- mongodb-data: Persists MongoDB data across container restarts or removals.
- mongodb-log: Persists MongoDB logs for troubleshooting and auditing.
The MongoDB service is configured with a custom Docker network (mongodb_network
), using a static IP (172.19.0.2
) to ensure consistent access from dependent services.
The following environment variables are used to configure MongoDB and Mongo Express credentials securely:
# MongoDB Root Credentials
MONGO_INITDB_ROOT_USERNAME=admin
MONGO_INITDB_ROOT_PASSWORD=adminpassword
# Mongo Express Admin Credentials
MONGO_EXPRESS_USERNAME=expressadmin
MONGO_EXPRESS_PASSWORD=expresspassword
Docker Compose ensures that resource usage is optimized by using persistent volumes for data and logs. To manage resources efficiently:
- Monitor the
mongodb-data
andmongodb-log
volumes to prevent running out of disk space. - Adjust the memory and CPU settings in Docker Compose if required, depending on the system's available resources.
- Clone or download the repository.
- Navigate to the project directory.
- Create a
.env
file with your MongoDB and Mongo Express credentials. - Run the following command to start the services:
docker-compose up -d
This will start the MongoDB and Mongo Express services in detached mode.
-
MongoDB Not Starting:
- Check logs for errors related to the database:
docker logs mongodb
- Check logs for errors related to the database:
-
Authentication Issues:
- Verify the credentials in the
.env
file. - Make sure that Mongo Express is correctly configured with the admin credentials.
- Verify the credentials in the
-
Network Issues:
- Ensure that the IP subnet defined for
mongodb_network
does not conflict with other network configurations.
- Ensure that the IP subnet defined for
This project is licensed under the MIT License.
π MongoDB & Mongo-Express Deployment via Docker Compose
This output confirms that MongoDB and Mongo-Express have been successfully deployed using Docker Compose. Here are the key observations and recommendations:
β Successful Execution
Docker & Docker Compose Check
Image Pull & Build Process
mongo
,mongo-express
) were successfully pulled from Docker Hub.Container & Network Setup
mongodb_mongodb_network
has been successfully created.docker ps
output confirms that both containers are up and running. ππ Recommendations & Best Practices
Deprecation Warning: Remove
version
Attribute indocker-compose.yml
version
attribute is no longer required in newer Docker Compose versions.version: "3.8"
or similar in yourdocker-compose.yml
.Verify Mongo-Express Accessibility
Confirm MongoDB Connectivity
docker exec -it mongodb mongosh
Consider Persistent Storage for MongoDB
/data/db
directory.π― Final Thoughts
β Deployment is successful, containers are running as expected.
β Network and port bindings are correctly configured.
πΉ Fix the
version
deprecation warning for future-proofing.πΉ Ensure MongoDB persistence if required.
π‘ Great work! Just a few tweaks, and you're all set for production! ππ₯