How to Install MongoDB with Docker

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

Latest Articles