How to Install Docker and Setup MongoDB on Docker

In this article, we’ll guide you through the installation of Docker, setting up MongoDB inside a Docker container, restarting the MongoDB service, and connecting to it using Mongo Shell. Whether you’re a developer or a system administrator, using Docker for MongoDB is a great way to manage your database in a lightweight and efficient containerized environment.

Prerequisites

  • Windows 11, Linux, or macOS
  • Administrative privileges on your machine
  • Basic understanding of terminal or command prompt

1. Install Docker

For Windows 11:

  1. Download Docker Desktop from the official Docker website https://www.docker.com/products/docker-desktop/
  2. Install Docker Desktop: Follow the installation wizard, ensuring that the option to install the WSL 2 engine is checked.
  3. Launch Docker: After installation, launch Docker Desktop. You should see a whale icon in your system tray when Docker is running.

For Linux (Ubuntu):

  1. Update your System:
sudo apt-get update
sudo apt-get upgrade

2. Install Docker

sudo apt-get install docker.io

3. Start Docker

sudo systemctl start docker
sudo systemctl enable docker

For MacOS:

  1. Download Docker Desktop from the official Docker website https://www.docker.com/products/docker-desktop/
  2. Install Docker Desktop: Follow the installation instructions, then launch Docker from Applications.

2. Set Up MongoDB on Docker

Once Docker is installed , Follow these steps to setup mongoDB in a docker Container.

Step 1: Pull the MongoDB Docker Image

The first step is to pull the official MongoDB image from Docker Hub.

docker pull mongo

Step 2: Run MongoDB in a Container

Now, let’s start MongoDB inside a container. Use the following command:

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

Explaination:

  • --name mongodb-container: The name you want to give to your MongoDB container.
  • -d: Run the container in detached mode (in the background).
  • -p 27017:27017: Maps MongoDB’s default port (27017) to your host machine, allowing you to access MongoDB.

Step 3: Check if MongoDB is Running

To verify if MongoDB is running, use the following command:

docker ps

This will list all running containers. You should see your MongoDB container in the list.


3. How to Restart MongoDB on Docker

If you ever need to restart MongoDB, Docker makes it easy.

Step 1: Stop the MongoDB Container

To stop the container:

docker stop mongodb-container

Step 2: Start the MongoDB Container Again

To start MongoDB again:

docker start mongodb-container

Step 3: Restart MongoDB in a Single Command

You can also use the restart command:

docker restart mongodb-container

4. Connect to MongoDB using Mongo Shell

MongoDB provides an interactive shell to communicate with the MongoDB server. Here’s how to connect MongoDB running inside a Docker container using Mongo Shell.

Step 1: Install Mongo Shell on Your Local Machine

First, you need to have Mongo Shell installed on your local machine.

For Ubuntu:

sudo apt-get install mongodb-clients

For Windows and macOS:

You can download the Mongo Shell from the MongoDB Download Center.

Step 2: Connect to MongoDB in the Docker Container

Once you have Mongo Shell installed, connect to MongoDB running inside the Docker container using the following command:

mongo --host localhost --port 27017

This connects to the MongoDB server running on your local machine at port 27017.


5. Useful Docker Commands for MongoDB

Here are some useful commands that will help you manage MongoDB on Docker.

To View MongoDB Logs

docker logs mongodb-container

To Stop MongoDB Container

docker stop mongodb-container

To Start MongoDB Container

docker start mongodb-container

To Remove MongoDB Container

If you want to remove the MongoDB container completely:

docker rm mongodb-container

Conclusion

Setting up MongoDB inside a Docker container is a straightforward process and provides a flexible, isolated environment for managing your databases. With Docker, you can easily restart your MongoDB service, view logs, and connect to the database using Mongo Shell.

By following the steps outlined above, you should now have a fully functioning MongoDB instance running in Docker. Feel free to scale your setup further, experiment with more MongoDB configurations, or connect your MongoDB instance to other services in your development environment.

One Comment

  1. Hi, this is a comment.
    To get started with moderating, editing, and deleting comments, please visit the Comments screen in the dashboard.
    Commenter avatars come from Gravatar.

Leave a Comment

Your email address will not be published. Required fields are marked *