Skip to content

Instantly share code, notes, and snippets.

@farunurisonmez
Last active March 24, 2025 09:02
Show Gist options
  • Save farunurisonmez/ed9f49639faecef92c9a7e5705c1ced5 to your computer and use it in GitHub Desktop.
Save farunurisonmez/ed9f49639faecef92c9a7e5705c1ced5 to your computer and use it in GitHub Desktop.
Docker Compose Configuration for Mongo 6 on Ubuntu 24.04.1 with Static IP and Custom Settings
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} # Root kullanΔ±cΔ± adΔ±
MONGO_INITDB_ROOT_PASSWORD: ${MONGO_INITDB_ROOT_PASSWORD} # Root şifresi
MONGO_DB_BIND_IP: 0.0.0.0 # Bağlanılabilir IP
MONGO_DB_CACHE_SIZE_GB: 2 # WiredTiger cache boyutu
MONGO_DB_LOG_PATH: /var/log/mongodb/mongodb.log # Log dosyasΔ± yolu
ports:
- "27017:27017" # MongoDB portu
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} # Mongo Express admin kullanΔ±cΔ± adΔ±
ME_CONFIG_MONGODB_ADMINPASSWORD: ${MONGO_INITDB_ROOT_PASSWORD} # Mongo Express admin şifresi
ME_CONFIG_MONGODB_PORT: 27017 # MongoDB portu
ME_CONFIG_MONGODB_SERVER: 'mongodb' # MongoDB sunucu adΔ±
ME_CONFIG_BASICAUTH_USERNAME: ${MONGO_EXPRESS_USERNAME} # Mongo Express giriş kullanıcı adı
ME_CONFIG_BASICAUTH_PASSWORD: ${MONGO_EXPRESS_PASSWORD} # Mongo Express giriş şifresi
ports:
- "8081:8081" # Mongo Express web arayΓΌzΓΌ portu
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" # IP subnet ayarΔ±

Dockerized MongoDB 6 Service on Ubuntu 24.04.1 (AMD64)

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.

Table of Contents

  1. System Requirements
  2. Project Structure
  3. Technologies Used
  4. PostgreSQL Configuration
  5. Docker Configuration
  6. Volume Management
  7. Network Configuration
  8. Environment Variables
  9. Resource Management
  10. Deployment and Start Instructions
  11. Troubleshooting
  12. License

1. System Requirements

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

2. Project Structure

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.

3. Technologies Used

  • 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.

4. PostgreSQL Configuration

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.


5. Docker Configuration

This section describes the Docker configuration for MongoDB and Mongo Express.

docker-compose.yml

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"

6. Volume Management

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.

7. Network Configuration

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.


8. Environment Variables

The following environment variables are used to configure MongoDB and Mongo Express credentials securely:

.env File

# 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

9. Resource Management

Docker Compose ensures that resource usage is optimized by using persistent volumes for data and logs. To manage resources efficiently:

  • Monitor the mongodb-data and mongodb-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.

10. Deployment and Start Instructions

  1. Clone or download the repository.
  2. Navigate to the project directory.
  3. Create a .env file with your MongoDB and Mongo Express credentials.
  4. Run the following command to start the services:
    docker-compose up -d

This will start the MongoDB and Mongo Express services in detached mode.


11. Troubleshooting

  1. MongoDB Not Starting:

    • Check logs for errors related to the database:
      docker logs mongodb
  2. Authentication Issues:

    • Verify the credentials in the .env file.
    • Make sure that Mongo Express is correctly configured with the admin credentials.
  3. Network Issues:

    • Ensure that the IP subnet defined for mongodb_network does not conflict with other network configurations.

12. License

This project is licensed under the MIT License.

MONGO_INITDB_ROOT_USERNAME=testuser
MONGO_INITDB_ROOT_PASSWORD=testpassword
MONGO_EXPRESS_USERNAME=testadmin
MONGO_EXPRESS_PASSWORD=testpassword
MONGO_DB_AUTH=true
MONGO_DB_BIND_IP=0.0.0.0
MONGO_DB_MAX_CONNECTIONS=1000
MONGO_DB_CACHE_SIZE_GB=2
MONGO_DB_LOG_PATH=/var/log/mongodb/mongo.log
MONGO_DB_LOG_APPEND=true
MONGO_DB_REPL_SET=rs0
@farunurisonmez
Copy link
Author

farunurisonmez commented Mar 24, 2025

image

πŸš€ 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

  1. Docker & Docker Compose Check

    • Docker version 28.0.2 and Compose version 2.34.0 are used. These versions are fairly recent and should work fine.
  2. Image Pull & Build Process

    • All required images (mongo, mongo-express) were successfully pulled from Docker Hub.
    • No errors were encountered during the build process.
  3. Container & Network Setup

    • Network: mongodb_mongodb_network has been successfully created.
    • Containers:
      • MongoDB is running on port 27017.
      • Mongo-Express (the web UI for MongoDB) is running on port 8081.
    • The docker ps output confirms that both containers are up and running. πŸš€

πŸ” Recommendations & Best Practices

  1. Deprecation Warning: Remove version Attribute in docker-compose.yml

    • The warning suggests that the version attribute is no longer required in newer Docker Compose versions.
    • Action: Remove the line specifying version: "3.8" or similar in your docker-compose.yml.
  2. Verify Mongo-Express Accessibility

    • Open your browser and check: http://localhost:8081/
    • If you face issues, inspect the logs:
      docker logs mongo-express
  3. Confirm MongoDB Connectivity

    • Use the following command to enter the MongoDB shell and verify database access:
      docker exec -it mongodb mongosh
    • If MongoDB isn’t accessible, debug with:
      docker logs mongodb
  4. Consider Persistent Storage for MongoDB

    • If persistence is needed, ensure a named volume is mapped to MongoDB's /data/db directory.
    • Example configuration:
      volumes:
        - mongo_data:/data/db  
    • Without this, data will be lost if the container restarts.

🎯 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! πŸš€πŸ”₯

@farunurisonmez
Copy link
Author

image

@farunurisonmez
Copy link
Author

image
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment