Install MongoDB using Docker on Windows

Install MongoDB using Docker

MongoDB is an open-source document-based NoSQL database that is widely used by many developers and organizations. It provides a flexible data model, rich query language, and high-performance indexing. However, setting up and managing MongoDB can be a complex and time-consuming process, especially when dealing with dependencies and configurations. Using Docker can simplify the installation of MongoDB.

In this tutorial, we will show you how to install MongoDB using Docker on Windows.

Tools and Technologies

  • Microsoft Windows 10
  • Docker Desktop 4.21.1
  • MongoDB Community Edition 6.0.8

Step 1: Install Docker

Before we start, you need to have Docker installed on your Windows computer. You can download and install Docker Desktop for Windows from the official Docker website. Once you have Docker installed, you can proceed to the next step.

Step 2: Download MongoDB Docker Image

You can download the latest MongoDB Docker image from the MongoDB official Docker Hub page.

MongoDB official Docker hub page

To download the MongoDB Docker image, open the Command Prompt window and run the following command:

docker pull mongodb/mongodb-community-server:6.0.8-ubuntu2204

This command will download the MongoDB community edition version 6.0.8 6.0.8-ubuntu2204 Docker image to your local machine repository.

Step 3: Run MongoDB Container

Once the MongoDB Docker image is downloaded, you can create and launch a MongoDB container using the following command:

docker run --name mongodb6 -d mongodb/mongodb-community-server:6.0.8-ubuntu2204

This command creates a new MongoDB container named mongodb6. The -d option instructs Docker to run the container in the background.

To access MongoDB locally from another application, you can expose port 27017 with -p option.

docker run --name mongodb6 -p 27017:27017 -d mongodb/mongodb-community-server:6.0.8-ubuntu2204

If you want to keep and store the data on your local machine under the directory C:/Users/chankok/data, you can mount a volume using the -v option.

docker run --name mongodb6 -p 27017:27017 -v C:/Users/chankok/data:/data/db -d mongodb/mongodb-community-server:6.0.8-ubuntu2204

You can utilize the environment variables MONGODB_INITDB_ROOT_USERNAME and MONGODB_INITDB_ROOT_PASSWORD to initialize the root user and password for your MongoDB server.

docker run --name mongodb6 -p 27017:27017 -e MONGODB_INITDB_ROOT_USERNAME=mongodb -e MONGODB_INITDB_ROOT_PASSWORD=password -d mongodb/mongodb-community-server:6.0.8-ubuntu2204

Step 4: Verify MongoDB Container

To verify the successful launch of the MongoDB container, execute the following command:

docker ps

This command will display a list of all running containers on your computer. You should see the MongoDB container listed in the output.

CONTAINER ID   IMAGE                                               COMMAND                  CREATED        STATUS                 PORTS                      NAMES
55b972d17383   mongodb/mongodb-community-server:6.0.8-ubuntu2204   "python3 /usr/local/…"   2 weeks ago    Up 7 hours             0.0.0.0:27017->27017/tcp   mongodb6

Step 5: Connect to MongoDB with MongoDB Shell

Utilize the MongoDB Shell mongosh to establish a connection with the MongoDB server within the container. Execute the following command:

docker exec -it mongodb6 mongosh --username mongodb --password password

This command initiates a connection using the provided root user credentials. The output should resemble:

Current Mongosh Log ID: 64cf5d20f25385126ffdeffc
Connecting to:          mongodb://@127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+1.10.1
Using MongoDB:          6.0.8
Using Mongosh:          1.10.1

For mongosh info see: 

------
   The server generated these startup warnings when booting
   2023-08-06T00:33:43.082+00:00: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine. See 
   2023-08-06T00:33:44.161+00:00: /sys/kernel/mm/transparent_hugepage/enabled is 'always'. We suggest setting it to 'never'
   2023-08-06T00:33:44.161+00:00: vm.max_map_count is too low
------

Once connected, you can use the MongoDB shell to interact with the database.

You can run show dbs command to list all the existing databases.

test> show dbs
admin   100.00 KiB
config   12.00 KiB
local    72.00 KiB

Summary

Congratulations! You have successfully installed MongoDB using Docker on Windows environment. By using Docker, you can easily manage and run MongoDB on your machine without having to worry about dependencies or configuration issues. We hope this guide has been helpful and that you are now able to use MongoDB with Docker.

References