MongoDB on Your Local Machine Using Docker: A Step-by-Step Guide
Tags: docker, mongodb, localmachine, javascript
Published: 2024-01-01 08:08:47


### 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 

{% embed https://www.nileshblog.tech/mongodb-on-your-local-machine-using-docker/ %}

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

```yaml
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:

```bash
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:

```bash
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:

```bash
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.

    ```yaml
    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](https://docs.docker.com/)
2. [MongoDB Official Documentation](https://docs.mongodb.com/)
3. [Docker Compose Documentation](https://docs.docker.com/compose/)
4. [MongoDB on Your Local Machine Using Docker: A Step-by-Step Guide](https://www.nileshblog.tech/mongodb-on-your-local-machine-using-docker/)

***Your support will help me continue to bring new content. Love Coding! 🚀***