MongoDB on Your Local Machine Using Docker: A Step-by-Step Guide

Nilesh RautNilesh Raut
3 min read

Introduction

In the ever-evolving landscape of database management, MongoDB stands tall as a reliable choice for developers. In this guide, we'll explore the seamless integration of MongoDB into your local environment using Docker on Ubuntu. Follow the step-by-step instructions to ensure a smooth setup and unleash the potential of MongoDB without the typical installation hassles.

####To install on Windows : Install Desktop Docker Application

Docker Compose File (docker-compose.yml)

Let's kick off by deciphering the docker-compose.yml file, a crucial element in our MongoDB-Docker integration journey.

version: "3.1"

services:
  mongo:
    image: mongo
    restart: always
    container_name: mongo
    ports:
      - 27017:27017
    environment:
      MONGO_INITDB_ROOT_USERNAME: NileshBlog.Tech
      MONGO_INITDB_ROOT_PASSWORD: SpeakLouder
    volumes:
      - ./db_data/:/data/db/
  • version: "3.1": Denotes the Docker Compose file version being used.

  • services: Defines the services we're using in our Docker setup.

  • mongo: Service name for our MongoDB container.

  • image: mongo: Specifies the MongoDB Docker image to be used.

  • restart: always: Ensures that the MongoDB container restarts automatically in case of failure.

  • container_name: mongo: Assigns a specific name to our MongoDB container for easy identification.

  • ports: - 27017:27017: Maps the host machine's port 27017 to the MongoDB container's port 27017.

  • environment: Sets up the initial MongoDB root username and password.

  • volumes: - ./db_data/:/data/db/: Links a local directory (./db_data/) to the MongoDB container's data directory.

Installing MongoDB on Your Local Machine Using Docker

Now, let's dive into the installation process.

Step 1: Create a Docker Compose File

Begin by creating a file named docker-compose.yml and copy the aforementioned YAML configuration into it.

Step 2: Run Docker Compose

Execute the following command in the terminal:

docker-compose up -d

This command will initiate the MongoDB container in detached mode, allowing you to continue working without interruptions.

Step 3: Verify the Installation

Ensure MongoDB is up and running by accessing it via the MongoDB shell:

docker exec -it mongo mongo -u Nilesh -p raut --authenticationDatabase admin

You are now connected to your MongoDB instance!

Connecting to MongoDB Docker Container

To interact with your MongoDB container, you need to connect to it from your application or a MongoDB client. Use the following connection string:

mongodb://Nileshblog.tech:dev.to/speaklouder@localhost:27017/admin

Replace Nileshblog.tech and dev.to/speaklouder with your specified username and password.

Tips for Efficient MongoDB Docker Usage

  1. Data Persistence: Leverage Docker volumes for data persistence, ensuring your data survives container restarts.

     volumes:
       - ./db_data/:/data/db/
    
  2. Customize Authentication: Modify the MONGO_INITDB_ROOT_USERNAME and MONGO_INITDB_ROOT_PASSWORD in the docker-compose.yml file for enhanced security.

  3. Mapping Ports: Adjust the port mapping in case port 27017 is already in use on your machine.

Conclusion

In conclusion, deploying MongoDB on your local machine using Docker provides a flexible and efficient development environment. Follow the steps outlined in this guide to set up MongoDB seamlessly on Ubuntu. Embrace the power of Docker to simplify your MongoDB installation and enhance your development workflow.

Trusted Reference Sources:

  1. Docker Documentation
  2. MongoDB Official Documentation
  3. Docker Compose Documentation
  4. MongoDB on Your Local Machine Using Docker: A Step-by-Step Guide

Your support will help me continue to bring new content. Love Coding! ๐Ÿš€

0
Subscribe to my newsletter

Read articles from Nilesh Raut directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Nilesh Raut
Nilesh Raut

Software engineer @jio , Founder of Nileshblog.tech & Technilesh.com and More .