Skip to main content

Command Palette

Search for a command to run...

Docker Compose file for MongoDB

Useful docker compose file to use

Updated
2 min read
Docker Compose file for MongoDB
R

Full-stack developer with mastery across mobile (React Native, Ionic), front-end (React, Svelte, Vue.js), back-end (Express.js, FastAPI, Django), e-commerce (Shopify, OpenCart), and CMS (Wordpress, Laravel, Drupal, headless). Building web and mobile applications that deliver!

version: '3'

services:
  mongodb:
    image: mongo:latest
    container_name: mongodb_container
    ports:
      - "27017:27017"
    volumes:
      - ./mongodb-data:/data/db
    environment:
      - MONGO_INITDB_ROOT_USERNAME=admin
      - MONGO_INITDB_ROOT_PASSWORD=adminpassword
    networks:
      - mongo_network

networks:
  mongo_network:
    driver: bridge

In the world of containerization, Docker Compose stands as a powerful tool that simplifies the management and orchestration of multi-container applications. It allows you to define and configure all the services that make up your application within a single YAML file, streamlining deployment and management processes.

Short explanation of a Docker Compose File for MongoDB above

Let's dive into a specific Docker Compose file designed to deploy a MongoDB database, exploring its key components and configurations:

1. Version Declaration:

  • version: '3' indicates the file's adherence to the version 3 syntax of Docker Compose, ensuring compatibility with the appropriate Compose tool version.

2. MongoDB Service:

  • services: mongodb: introduces the definition of a service named "mongodb," representing the MongoDB database container.

3. Image Specification:

  • image: mongo:latest instructs Docker Compose to pull and utilize the latest official MongoDB image from Docker Hub, providing the foundation for the container.

4. Container Naming:

  • container_name: mongodb_container assigns a user-friendly name to the container, facilitating identification and management.

5. Port Mapping:

  • ports: - "27017:27017" establishes a port mapping between the host machine's port 27017 and the container's port 27017, enabling external access to the MongoDB database.

6. Persistent Data Storage:

  • volumes: - ./mongodb-data:/data/db creates a volume, linking the mongodb-data directory on the host machine to the /data/db directory within the container. This ensures that MongoDB's data persists even after container restarts or recreations.

7. Environmental Variables:

  • environment: sets specific environment variables within the container:

    • MONGO_INITDB_ROOT_USERNAME=admin designates the root user's username for initial database setup.

    • MONGO_INITDB_ROOT_PASSWORD=adminpassword assigns a password to the root user.

8. Network Configuration:

  • networks: - mongo_network attaches the MongoDB container to a custom network named "mongo_network," potentially enabling secure communication and isolation from other containers.

9. Network Creation:

  • networks: defines the "mongo_network" network with the driver: bridge option, establishing a bridge network for container communication.

Additional Considerations:

  • For production environments, consider using specific MongoDB image versions instead of latest for stability.

  • Explore advanced network options for security and isolation.

  • Integrate health checks and logging for monitoring and troubleshooting.