How to Install MongoDB with Docker

MongoDB is one of the most popular NoSQL databases in the world, offering flexibility, performance, and high scalability. MongoDB can store and manage both structured and unstructured data, such as documents, graphics, media, and more. MongoDB also supports advanced features, such as indexation, aggregation, replication, sharding, and transactions.

Docker is a software platform that allows you to create, run, and distribute applications using containers. A container is a unit of software isolated from the surrounding environment, which contains everything an application needs to function, such as code, libraries, dependencies, and configurations. With Docker, you can ensure that your applications can run smoothly anywhere, without worrying about compatibility or dependencies.

Installing MongoDB with Docker is an easy and fast way to create a MongoDB database on your computer. You don’t need to manually download or install MongoDB, as Docker will handle all the processes for you. You can also take advantage of Docker features such as volume, networking, and composition to better manage your MongoDB database.

MongoDB

In this article, Bardimin will explain step by step how to install MongoDB with Docker on Windows. I will also provide some tips and tricks to optimize the use of MongoDB with Docker.

Requirement

Before you install MongoDB with Docker, you need to meet the following requirements:

  • You must have Docker installed on your computer. If you don’t have Docker yet, you can download and install it from the official Docker website.
  • You must have a stable internet connection to download MongoDB images from Docker Hub.
  • You must have enough disk space to store MongoDB images and data.

Install MongoDB with Docker on Windows

To install MongoDB with Docker on Windows, follow these steps:

  1. Open PowerShell or Command Prompt as an administrator.
  2. Run the following command to create a new directory on drive C: that will be used as a volume to store MongoDB data:
mkdir C:\data\db
  1. Run the following command to run a MongoDB container named mongo using a mongo image from Docker Hub:
docker run --name mongo -v C:\data\db:/data/db -d mongo

This command will do:

  1. –name mongo assigns the mongo name to the container.
  2. -v C:\data\db:/data/db links the C:\data\db directory on the host to the /data/db directory on the container as a volume. This volume will be used by MongoDB to store data.
  3. -d runs the container in the background (detached mode).
  4. Mongo specifies the image used to create the container.
  5. Wait, a few moments for the container to finish creating and running. You can check the status of the container by running the following command:
docker ps

This command will display a list of containers currently running on your machine. You should see an output like this:

CONTAINER ID   IMAGE     COMMAND                  CREATED         STATUS         PORTS       NAMES
5f0b9c8a9f0a   mongo     "docker-entrypoint.s…"   2 minutes ago   Up 2 minutes   27017/tcp   mongo

This output shows that the mongo container is running with a mongo image and port 27017 (MongoDB’s default port).

  1. Congratulations, you have successfully installed MongoDB with Docker on Windows! You can now connect to your MongoDB database using tools you love, such as MongoDB Shell, MongoDB Compass, or MongoDB drivers for your favorite programming language.

Installing MongoDB with Docker on Linux

To install MongoDB with Docker on Linux, follow these steps:

  1. Open a terminal and log in as root or a user with sudo rights.
  2. Run the following command to create a new directory in /data/db that will be used as a volume to store MongoDB data:
sudo mkdir -p /data/db
  1. Run the following command to run a MongoDB container named mongo using a mongo image from Docker Hub:
sudo docker run --name mongo -v /data/db:/data/db -d mongo

This command will do the same things as described in the previous section for Windows.

  1. Wait a few moments for the container to finish creating and running. You can check the status of the container by running the following command:
sudo docker ps

This command will display a list of containers currently running on your machine. You should see output like this:

CONTAINER ID   IMAGE     COMMAND                  CREATED         STATUS         PORTS       NAMES
5f0b9c8a9f0a   mongo     "docker-entrypoint.s…"   2 minutes ago   Up 2 minutes   27017/tcp   mongo

This output indicates that the mongo container is running with a mongo image and port 27017 (MongoDB’s default port).

  1. Congratulations, you have successfully installed MongoDB with Docker on Linux! You can now connect to your MongoDB database using tools you love, such as MongoDB Shell, MongoDB Compass, or MongoDB drivers for your favorite programming language.

Tips and Tricks

Here are some tips and tricks that can help you optimize your use of MongoDB with Docker:

  • If you want to stop the MongoDB container, you can run the following command:
docker stop mongo
  • If you want to delete a MongoDB container, you can run the following command:
docker rm mongo
  • If you want to delete a MongoDB image, you can run the following command:
docker rmi mongo
  • If you want to specify the specific version of MongoDB you want to use, you can add a version tag after the image name. For example, if you want to use version 5.0 of MongoDB, you can run the following command:
docker run --name mongo -v /data/db:/data/db -d mongo:5.0
  • If you want to expose the container port to a different host port, use the -p option. For example, if you want to expose port 27017 from a container to port 3000 from the host, you can run the following command:
docker run --name mongo -v /data/db:/data/db -p 30000:27017 -d mongo
  • If you want to run multiple instances of MongoDB on the same machine, you must provide different names for each container and expose different ports for each container. For example, if you want to run two MongoDB instances named mongo1 and mongo2, and expose ports 27017 and 27018 from the host, respectively, you can run the following commands:
docker run --name mongo1 -v /data/db1:/data/db -p 27017:27017 -d mongo
docker run --name mongo2 -v /data/db2:/data/db -p 27018:27017 -d mongo
  • If you want to use Docker Compose to manage your MongoDB containers more easily, you can create a docker-compose.yml file with contents like this:
```
version: "3"
services:
  mongo:
    image: mongo
    volumes:
      - /data/db:/data/db
    ports:
      - 27017:27017
```
  • If you want to connect MongoDB with other applications running in Docker containers, you can use Docker’s internal network to allow communication between containers. For example, if you want to connect MongoDB with a Node.js application running in an app container, you can create a docker-compose.yml file with contents like this:
version: "3"
services:
  mongo:
    image: mongo
    volumes:
      - /data/db:/data/db
    networks:
      - app-network
  app:
    image: node
    volumes:
      - /app:/app
    ports:
      - 3000:3000
    networks:
      - app-network
networks:
  app-network:
  • If you want to configure MongoDB in more detail, use a custom configuration file containing the options you want. For example, if you want to enable authentication and encryption for MongoDB, you can create a mongod.conf file with contents like this:
systemLog:
  destination: file
  path: "/var/log/mongodb/mongod.log"
  logAppend: true
storage:
  dbPath: "/data/db"
  journal:
    enabled: true
net:
  port: 27017
  bindIp: 0.0.0.0
  tls:
    mode: requireTLS
    certificateKeyFile: "/etc/ssl/mongodb.pem"
security:
  authorization: enabled
  • If you want to use that configuration file, you must copy it to a directory that the MongoDB container can access, and provide the path of that file as an argument when running the container. For example, if you copy the mongod.conf file to the /etc/mongo directory on the host, you can run the following command:
docker run --name mongo -v /etc/mongo:/etc/mongo -v /data/db:/data/db -d mongo --config /etc/mongo/mongod.conf

Conclusion

Using Docker, you can create a MongoDB database easily and quickly, without worrying about compatibility or dependencies. You can also take advantage of Docker features such as volume, networking, and composition to better manage your MongoDB database.

I hope this article was useful for those of you who want to learn about how to install MongoDB with Docker. Thanks for reading!

Latest Articles