Docker Tutorial

Docker Installation

How to use Docker

Docker Instance

Docker Container Lifecycle Command

Docker Container Operation Command

Docker Container rootfs Command

Docker Mirror Repository Command

Docker Local Image Management Command

Docker info|version Command

Docker install MongoDB

In this tutorial, you'll learn how to create a Docker container running MongoDB, a popular NoSQL database. We'll use the official MongoDB image provided by the Docker community.

  1. Prerequisites:

    • Install Docker on your system
  2. Pull the official MongoDB image:

    To pull the official MongoDB image, run the following command:

    docker pull mongo
    

    This will download the latest version of the MongoDB image to your system.

  3. Create a new container with MongoDB:

    To create a new Docker container running MongoDB, use the docker run command. We'll map the container's port 27017 (the default MongoDB port) to the host's port 27017, so you can access the MongoDB server from your host machine:

    docker run --name mongo-container -p 27017:27017 -d mongo
    

    This command creates a new container named mongo-container, maps the container's port 27017 to the host's port 27017, and runs the container in detached mode (-d flag). The container will run the MongoDB server using the default configuration.

  4. Verify the MongoDB container is running:

    To check if the MongoDB container is running, use the docker ps command:

    docker ps
    

    You should see your mongo-container listed as running.

  5. Connect to the MongoDB server:

    You can connect to the MongoDB server running inside the container using a MongoDB client or driver. If you have the MongoDB command line client (mongo) installed on your host machine, you can connect to the server using:

    mongo --host localhost --port 27017
    

    This command connects to the MongoDB server running at localhost:27017, which is the mapped port of the mongo-container.

  6. Configure MongoDB authentication (Optional):

    By default, the MongoDB server in the container runs without authentication. To enable authentication, you'll need to create an admin user and restart the container with authentication enabled.

    First, connect to the MongoDB server as described in step 5.

    Then, create the admin user by running the following commands inside the mongo shell:

    use admin
    db.createUser(
      {
        user: "admin",
        pwd: "your_secure_password",
        roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
      }
    )
    

    Replace "your_secure_password" with a secure password for the admin user.

    Exit the mongo shell by typing exit.

    Stop and remove the existing mongo-container:

    docker stop mongo-container
    docker rm mongo-container
    

    Finally, create a new container with authentication enabled:

    docker run --name secure-mongo-container -p 27017:27017 -d mongo --auth
    

    This command creates a new container named secure-mongo-container, maps the container's port 27017 to the host's port 27017, runs the container in detached mode, and starts the MongoDB server with authentication enabled.

    To connect to the MongoDB server with authentication enabled, use the following command:

    mongo --host localhost --port 27017 -u admin -p your_secure_password --authenticationDatabase admin
    

    Replace your_secure_password with the password you set for the admin user.

In this tutorial, we covered how to create a Docker container running MongoDB, access the server from your host machine, and configure MongoDB authentication. You can now use this containerized MongoDB server for development, testing, or deployment purposes.

  1. Running MongoDB in Docker Container:

    • Description: Run MongoDB in a Docker container for a lightweight and isolated database environment.
    • Code Example:
      docker run -d -p 27017:27017 --name my-mongodb-container mongo:latest
      
  2. How to Create a Docker Image with MongoDB:

    • Description: Create a custom Docker image with MongoDB installed using a Dockerfile.
    • Code Example (Dockerfile):
      FROM mongo:latest
      # Add custom configurations or scripts
      
  3. Docker-Compose MongoDB Installation:

    • Description: Use Docker Compose to define and run multi-container Docker applications, including MongoDB.
    • Code Example (docker-compose.yml):
      version: '3'
      services:
        mongodb:
          image: mongo:latest
          ports:
            - "27017:27017"
      
  4. MongoDB Dockerfile Example:

    • Description: A Dockerfile example for creating a custom MongoDB image with additional configurations or scripts.
    • Code Example (Dockerfile):
      FROM mongo:latest
      COPY my-mongod.conf /etc/mongod.conf
      
  5. Setting Up MongoDB in a Docker Environment:

    • Description: Configure MongoDB settings, such as authentication or data directories, within a Docker environment.
    • Code Example (docker-compose.yml):
      version: '3'
      services:
        mongodb:
          image: mongo:latest
          ports:
            - "27017:27017"
          environment:
            MONGO_INITDB_ROOT_USERNAME: root
            MONGO_INITDB_ROOT_PASSWORD: example
      
  6. Configuring MongoDB in Docker-Compose:

    • Description: Customize MongoDB configurations using Docker Compose for a multi-container environment.
    • Code Example (docker-compose.yml):
      version: '3'
      services:
        mongodb:
          image: mongo:latest
          ports:
            - "27017:27017"
          volumes:
            - ./mongo-data:/data/db
      
  7. Docker MongoDB Image Customization:

    • Description: Customize the MongoDB image by adding configurations, scripts, or creating a specific database environment.
    • Code Example (Dockerfile):
      FROM mongo:latest
      COPY my-scripts /scripts
      RUN chmod +x /scripts/setup.sh
      
  8. Docker MongoDB Volume Mounts:

    • Description: Use Docker volumes to mount external directories or data files into MongoDB containers for persistence.
    • Code Example (docker-compose.yml):
      version: '3'
      services:
        mongodb:
          image: mongo:latest
          volumes:
            - ./mongo-data:/data/db
      
  9. Custom MongoDB Image Creation in Docker:

    • Description: Create a custom MongoDB image with specific configurations and data preloaded for your application.
    • Code Example (Dockerfile):
      FROM mongo:latest
      COPY my-scripts /scripts
      RUN chmod +x /scripts/setup.sh
      
  10. Docker Swarm and MongoDB Service:

    • Description: Deploy MongoDB as a service in a Docker Swarm to scale and manage multiple MongoDB containers.
    • Code Example (docker-compose.yml):
      version: '3'
      services:
        mongodb:
          image: mongo:latest
          deploy:
            replicas: 3
            # Other Swarm deployment configurations
      
  11. Troubleshooting MongoDB in Docker:

    • Description: Troubleshoot MongoDB-related issues in Docker by examining logs, checking configurations, and verifying container settings.
    • Code Example:
      docker logs <mongodb_container_id_or_name>