w3resource

Installing MongoDB with Docker

Install MongoDB Community Edition with Docker

Using Docker to install MongoDB Community Edition provides a lightweight and portable way to manage your database. Docker eliminates the need for direct installation on your host system, making the setup process simpler and more scalable. This guide explains how to set up MongoDB with Docker, configure it, and troubleshoot common issues.

Step-by-Step Installation Guide

1. Pre-Installation Checklist

Before installing MongoDB with Docker, ensure the following:

  • Docker Installed: Docker must be installed on your system. Visit Docker's website to download and install Docker Desktop for your operating system.
  • System Requirements: Minimum 2GB of free memory and sufficient disk space for database files.

2. Pull the MongoDB Docker Image

a) Open Terminal

  • Ensure Docker is running on your system

b) Download the MongoDB Image

  • Run the following command to pull the latest MongoDB Community image:
docker pull mongo:latest

c) Verify the Image

  • Confirm the image is downloaded by running:
docker images

3. Run MongoDB in a Docker Container

a) Start a MongoDB Container

  • Use the following command to start a container:
docker run --name mongodb-container -d -p 27017:27017 -v mongodbdata:/data/db mongo:latest

Explanation:

  • --name mongodb-container: Names the container.
  • -d: Runs the container in detached mode.
  • -p 27017:27017: Maps MongoDB's default port to your host machine.
  • -v mongodbdata:/data/db: Creates a persistent volume for data storage.

b) Verify the Running Container

  • Check the running container:
docker ps

c) Access the MongoDB Shell

  • Use docker exec to access the shell:
docker exec -it mongodb-container mongo

4. Configure MongoDB with Docker

a) Customize Environment Variables

  • Pass environment variables to set a username and password:
docker run --name mongodb-secure -d -p 27017:27017 -v mongodbdata:/data/db \
-e MONGO_INITDB_ROOT_USERNAME=admin \
-e MONGO_INITDB_ROOT_PASSWORD=secret \
mongo:latest

b) Use a Custom Configuration File

  • Create a mongod.conf file with custom settings.
  • Mount it as a volume when running the container:
docker run --name mongodb-custom -d -p 27017:27017 -v /path/to/mongod.conf:/etc/mongod.conf mongo:latest --config /etc/mongod.conf

5. Managing MongoDB Containers

a) Stop a Running Container

  • Run the following command:
docker stop mongodb-container

b) Restart a Container

  • Restart MongoDB with:
docker start mongodb-container

c) Remove a Container

  • Stop and remove a container:

Code:

docker stop mongodb-container
docker rm mongodb-container

Troubleshooting Installation Issues

a) Port Conflict

  • If port 27017 is already in use, specify a different port:
docker run -d -p 28017:27017 mongo:latest

b) Container Exits Immediately

  • Check the logs to diagnose the issue:
docker logs mongodb-container

c) Data Loss After Restart

  • Ensure data is stored in a persistent volume (-v mongodbdata:/data/db).

Uninstalling MongoDB with Docker

1. Stop and Remove Containers

docker stop mongodb-container
docker rm mongodb-container

2. Delete MongoDB Images

docker rmi mongo:latest

3. Remove Persistent Volumes

docker volume rm mongodbdata

Additional Commands for Beginners

Code:

# Insert data into MongoDB via the shell
db.users.insertOne({ name: "Ruth", age: 30 })

# Retrieve all data from a collection
db.users.find()

# Update a document
db.users.updateOne({ name: "Ruth" }, { $set: { age: 31 } })

# Delete a document
db.users.deleteOne({ name:  “Ruth" }) 


Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/mongodb/installation-mongodb-docker.php