MongoDB Tutorial 3 [How to Install MongoDB as Docker Image on Windows]

MongoDB Tutorial 3 [How to Install MongoDB as Docker Image on Windows]

In this tutorial, we will learn about “How to install MongoDB as Docker image on Windows”. Docker, a containerization platform, provides a convenient and lightweight environment for running applications, making it an excellent choice for deploying MongoDB instances across various development and production environment. we’ll walk through the step-by-step process of installing MongoDB as a Docker image on a Windows system. So, let us get started.

 

Different ways to Install MongoDB 

I assume you have prior high-level knowledge on MongoDB. If you are new to MongoDB, I will recommend to first look at MongoDB Tutorial 1 [How to Install MongoDB on Windows] tutorial for better understanding of current tutorial. Let us look at and understand various methods that are available to install MongoDB on Windows. These methods are explained below.

  1. MSI Installer: MongoDB provides a Windows Installer package (MSI) for easy installation. You can download the MSI file from the MongoDB website mongodb.com and follow the installation wizard to install MongoDB on Windows machine.
  2. Zip Archive: MongoDB also offers a ZIP archive containing the binaries for manual installation. Again, you can download the ZIP file from the MongoDB website, extract its contents, and configure MongoDB manually.
  3. Chocolatey Package Manager: Chocolatey is a package manager for Windows that allows users to install software packages from the command line. MongoDB can be installed using Chocolatey by executing the command choco install mongodb.
  4. Windows Subsystem for Linux (WSL): You can install MongoDB on Windows using WSL, which allows running a Linux distribution alongside Windows. After setting up WSL, MongoDB can be installed on the Linux distribution running within WSL.
  5. Docker: Docker provides containers for running applications in isolated environments. You can run MongoDB in a Docker container on Windows by pulling the MongoDB image from Docker Hub and running it with Docker commands.

Each installation method offers its own advantages and may be suitable depending on the user’s preferences and requirements. We will focus on the last installation method which is to install MongoDB as Docker image. Covering all the installation methods are beyond the scope of this tutorial.

How to Install MongoDB as Docker Image on Windows 

So far, we have understood about different ways to install MongoDB on Windows. Let us now discuss in depth the method to install the MongoDB as Docker image on Windows. Please make sure prerequisite are met before you start following the installation steps given in this section.

Prerequisite

  • Windows operating system installed.
  • Docker installed.
  • Basic understanding of Windows commands.

 

Step-1: Check the Running Docker Version

The very first step is to check if Docker daemon is running or not. There are different ways to check the status of Docker daemon. We will use the most common command which is docker version. If the command output returns the installed docker version, it means the Docker daemon is running.

PS C:\Users\coder> docker version
Client:
Cloud integration: v1.0.35+desktop.5
Version: 24.0.7
API version: 1.43
...............................................................

Server: Docker Desktop 4.26.1 (131620)
Engine:
Version: 24.0.7
API version: 1.43 (minimum version 1.12)
..................................................................
GitCommit: v1.1.10-0-g18a0cb0
docker-init:
Version: 0.19.0
GitCommit: de40ad0

 

You can also execute the command Get-Process '<process-name>' to check if Docker daemon is running as shown below.

PS C:\Users\coder> Get-Process 'docker'

Handles NPM(K) PM(K) WS(K)  CPU(s) Id    SI  ProcessName
------- ------ ----- -----  ------ --    --  -----------
117     11     26808 25700  0.05   21904 1   docker

 

Step-2: Pull MongoDB Docker Image

In this step, we will pull the MongoDB official Docker image from the Docker hub using the command docker pull <image-name>:<tag> as shown below.

PS C:\Users\coder> docker pull mongo
Using default tag: latest
latest: Pulling from library/mongo
01007420e9b0: Pull complete
543cad33eff8: Pull complete
1510c0c9be17: Pull complete
544391ab0068: Pull complete
89fd5305ccb6: Pull complete
c9b10642422e: Pull complete
c0642ef7f936: Pull complete
16d217fa1c04: Pull complete
Digest: sha256:a064093062c2e97499cd88544d3afb1d86b00dbe94153984a07774857034631e
Status: Downloaded newer image for mongo:latest
docker.io/library/mongo:latest

What's Next?
View a summary of image vulnerabilities and recommendations → docker scout quickview mongo

 

Next, verify if the MongoDB docker image is pulled successfully by executing the command docker images as shown below.

PS C:\Users\coder> docker images
REPOSITORY TAG    IMAGE ID     CREATED      SIZE
mongo      latest 7f42cb1c7e98 41 hours ago 756MB

 

NOTE:

By default, docker pull mongo command pulls the latest MongoDB docker image. To install any specific version of MongoDB docker image, use the command docker pull mongo:<tag>.

 

Step-3: Create MongoDB Docker container

In this step, after pulling the MongoDB docker image, we will create a MongoDB container using the pulled Docker image by executing below command.

PS C:\Users\coder> docker run -td --name mongo-container mongo:latest
fd4c036248d6d209589eb110e77ff7b7ca369c992ecdf316f633a3ecfb2d6b06

 

Next, verify if the MongoDB container is created successfully by using below command.

PS C:\Users\coder> docker ps
CONTAINER ID IMAGE         COMMAND                CREATED       STATUS       PORTS     NAMES
fd4c036248d6 mongo:latest  "docker-entrypoint.s…" 3 seconds ago Up 2 seconds 27017/tcp mongo-container

 

NOTE:

All the Docker CLI commands given in this tutorial are executed on Windows PowerShell.

 

Step-4: Login to the MongoDB Container

We can also login to the container and perform all the MongoDB native operation like creating MongoDB databases, collections, documents etc. To login to the MongoDB container, execute below command where ‘mongo-container’ is the name given for new container which will be created. By default, the login to container happens as root user as shown below.

PS C:\Users\coder> docker exec -it mongo-container bash
root@fd4c036248d6:/# pwd
/

 

Step-5: Exit from MongoDB Container

To exit from the MongoDB container, execute the command exit. It will exit from the container and return back to Windows terminal as show below.

root@fd4c036248d6:~# exit
exit
PS C:\Users\coder>

 

Step-6: Delete the MongoDB Container

In this step, you have completed this tutorial, you can delete the running MongoDB container as well. To do so, you nedd to first stop the running container followed by deleting the container as shown below.

//stop mongo-container then remove mongo-container
PS C:\Users\coder> docker stop mongo-container;docker rm mongo-container
mongo-container
mongo-container

 

Next, verify if the MongoDB container is successfully deleted by executing the command docker ps. If the output does not return the MongoDB container, it means the container is deleted successfully as shown below.

PS C:\Users\coder> docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

 

Summary

We have successfully installed MongoDB as Docker image by following the steps given in this tutorial. We have also created MongoDB container using the pulled Docker image. To learn more about MongoDB, look at MongoDB Documentation.

Leave a Comment