• Post author:
  • Post category:MongoDB
  • Post last modified:May 9, 2024
  • Reading time:8 mins read
You are currently viewing Run MongoDB in Docker Container

Using Docker to run MongoDB can simplify the deployment process and make it easier to manage the database infrastructure. A Docker container is a single package that contains everything (necessary configurations or dependencies) that is required to run the application.

Advertisements

These containers help developers to run an application in an isolated and consistent environment hence, running services in a container has become de-facto these days. In this article, we will learn how to set up MongoDB in a docker container.

This article is part of our MongoDB Tutorial Guide, the guide includes the MongoDB concepts and the most used examples with tips and tricks.

Following are step-by-step instructions on how to install and run MongoDB in a docker container.

  • Step1 – Install Docker
  • Step2 – Pull MongoDB Docker Image
  • Step3 – Run the MongoDB Image
  • Step4 – Verify the Container is Running
  • Step5 – Connect the MongoDB to the Docker Container
  • Step6 – Validate MongoDB instance is Running

1. Validate Docker Installation for MongoDB

Validate whether Docker is already installed on your system by checking its version. If it’s not already installed, then it is required to have docker installed before configuring docker to run the MongoDB instance.


# Check docker version
docker version

The command shows the version information for the Docker client and server components installed on the system. Also, we have displayed the output of the server version along with the build information.

run MongoDB Docker container

If you don’t have docker installed, you can follow this link to install first.

2. Pull the Docker Image of MongoDB

The next action is to get the MongoDB image from the Docker Hub repository. The following command of Docker will get the latest image of MongoDB. Alternatively, you can also specify the version to get the specific version of the MongoDB image.


# Pull latest mongodb image 
docker pull mongo:latest

When we run this command, Docker searches for the mongo image on the Docker Hub registry and downloads it to the local machine along with all the dependence libraries/packages needed to execute the containerized MongoDB instance.

We can see from the below output, the above command gets all the libraries of MongoDB into the Docker container and the status verifies that the newer image for MongoDB is downloaded successfully.

set up MongoDB in a Docker

3 Run the MongoDB Image in Docker

Here, we use the Docker run command to launch a containerized MongoDB instance after the MongoDB image has been downloaded. The command is given below to run the image of MongoDB as a container.


# Run MongoDB image as a container
docker run -d --name mongodb -p 27017:27017 mongo 

Here, -d flag starts the container in the background, which means that the container will run in detached mode. Then, the  --name option is used to give a name to the new container, in this case, mongodb. After that, the -p flag maps the given port container to the set port of the host.  This enables us to access the MongoDB instance running inside the container using the host system’s IP address and port number.

This command will give the following output when executed successfully.

4. Verify the Container is Running

Now, we can validate whether the container is running or not by using the docker with the ps command. To view a complete list of the Docker containers that are active on the system right now, execute the following command.


# Docker containers list
docker ps

Yields below output.

5. Connect the MongoDB to the Docker Container

Finally, we can connect to the MongoDB shell from the Docker containers by using the following command.  


# Connecting MongoDB to docker container
docker exec –it mongodb mongosh

When we run this command, Docker starts a new process inside the running mongodb container, launches the MongoDB shell (mongosh), and connects to the MongoDB instance running inside the container to set up MongoDB in a Docker. Here, the exec option specifies running a command in a Docker container that is already executing.

The –it option runs the command in interactive mode and MongoDB is the name of the running container. The mongosh is the shell we want to run inside the container. Thus, we can see the mongosh shell is launched inside the container and is ready to accept commands.

set up MongoDB in a Docker

6. Validate the MongoDB Command is Running

Finally, run the MongoDB hello command to verify that commands are running inside the docker container. This is an important step that confirms MongoDB is successfully running in the Docker container.


# Validation
db.runCommand({hello:1})

This should yield the below output.

run MongoDB Docker container

7. Conclusion

In conclusion, we have explained each step in detail to install and run the MongoDB instance in Docker. Additionally, if you want to install MongoDB Enterprise with Docker, you can visit the following link.