๐Ÿš€ Setting Up MongoDB in Docker Using Docker Compose

in #docker โ€ข 19 days ago

๐Ÿš€ Setting Up MongoDB in Docker Using Docker Compose

MongoDB is a powerful NoSQL database that's great for handling large volumes of data. Running MongoDB in Docker simplifies the setup and management. This guide will walk you through setting up MongoDB in Docker using Docker Compose.

๐Ÿ“‹ Prerequisites

Make sure you have the following installed:

๐Ÿ› ๏ธ Step 1: Create a Docker Compose File

Start by creating a docker-compose.yml file. This file will define the services and configuration needed to run MongoDB.

version: '3.8'

services:
  mongodb:
    image: mongo:latest
    container_name: mongodb_container
    ports:
      - "27017:27017" # ๐ŸŒ Expose MongoDB on port 27017
    environment:
      MONGO_INITDB_ROOT_USERNAME: root # ๐Ÿ” Set the root username
      MONGO_INITDB_ROOT_PASSWORD: examplepassword # ๐Ÿ” Set the root password
    volumes:
      - mongo_data:/data/db # ๐Ÿ’พ Persist data to a Docker volume

volumes:
  mongo_data:
    driver: local

๐Ÿ—‚๏ธ Step 2: Directory Structure

Ensure your project structure looks something like this:

project-directory/
โ”‚
โ”œโ”€โ”€ docker-compose.yml

This simple setup will allow you to manage your MongoDB instance with ease.

๐Ÿƒโ€โ™‚๏ธ Step 3: Running MongoDB with Docker Compose

Navigate to your project directory and run the following command to start MongoDB:

docker-compose up -d
  • The -d flag runs the containers in the background.

๐Ÿ” Step 4: Accessing MongoDB

MongoDB will be accessible on localhost:27017. You can connect to it using the mongo shell, a MongoDB client, or an application.

To check the logs for the MongoDB container, use:

docker-compose logs -f mongodb

๐Ÿ“ฆ Step 5: Stopping and Removing Containers

To stop the MongoDB container, run:

docker-compose down

This will stop and remove the container but keep the data intact in the Docker volume. If you want to remove the volumes as well, use:

docker-compose down -v

๐ŸŽ‰ Conclusion

You're now set up to run MongoDB in Docker using Docker Compose! This setup is scalable and can be easily extended with more services in the same docker-compose.yml file.

Sort: ย 

"Setup your MongoDB instance with Docker Compose today! ๐Ÿš€ With this guide, you'll be able to run a powerful NoSQL database in no time. Don't miss out on lucrative delegation opportunities at https://botsteem.com - earn up to 30% APY by delegating your Steem Power to @steem.botto! Join our community and get up to 26% in liquid payments and BottoVotes. Head to our Discord server (https://discord.gg/NRxBjpmzNc) for more community-driven discussions!"

"Wow! ๐Ÿคฉ Great guide on setting up MongoDB in Docker using Docker Compose! ๐Ÿ‘ I'm loving the step-by-step instructions and the emphasis on simplifying database management. ๐Ÿ’ก Have you used this setup for a project before? ๐Ÿค” Would love to hear about your experiences with MongoDB in Docker! ๐Ÿ˜Š Also, don't forget to vote for @xpilar.witness by going to https://steemitwallet.com/~witnesses and helping us continue to improve the Steem ecosystem!" ๐Ÿ’•