How to run Mongodb with Docker and Docker-Compose

In this guide we are going to explore how to run Mongodb locally with docker and docker compose. This can be helpful if you want to run Mongodb locally without installing it in your server or if you want to run multiple versions of Mongodb seamlessly.

Also check:

# Prerequisites

To follow along, ensure you have the following:

  • Docker installed locally and permissions to use it to launch containers
  • Docker compose is installed locally
  • Basic knowledge of the command line

# Using the docker run command

We are going to use the docker run command to achieve our goal. The version of Mongodb that we want is mongo:5.0 the latest Mongodb version.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
$ docker run -d \
    --name my-mongodb \
    -p 27017:27017 \
    -e MONGO_INITDB_ROOT_USERNAME=citiizix \
    -e MONGO_INITDB_ROOT_PASSWORD=S3cret \
    mongo:5.0
Unable to find image 'mongo:5.0' locally
5.0: Pulling from library/mongo
7b1a6ab2e44d: Already exists
90eb44ebc60b: Pull complete
5085b59f2efb: Pull complete
c7499923d022: Pull complete
019496b6c44a: Pull complete
c0df4f407f69: Pull complete
351daa315b6c: Pull complete
557b07ecd9d7: Pull complete
a2dff157a5e3: Pull complete
07d83e88231b: Pull complete
Digest: sha256:07212fb304ea36b8c5a9e5694527f16deeb0b99f87fc60162dc15ab260bf8a2a
Status: Downloaded newer image for mongo:5.0
37c8bc5ba6ea6b4ed31f65d5f060d1963c0b3384fa438dcd23fc4170b2eeb28f

In the above command:

  • The -d instructs docker container to run as a detached process. It run container in background and print container ID
  • -p is for port mapping. We are instructing the container to expose the container port externally. Container port 6379 is mapped to host port 6379. That means the service can be accessed through localhost:6379
  • The mongo:5.0 defines the image that we are running. We are running mongo image version 5.0.
  • The -e argument is for the environment variables. The supplied environment variables will be used to set up a Mongo DB root user and password.

To check that our container is running as expected, use the docker ps command:

1
2
3
$ docker ps
CONTAINER ID   IMAGE       COMMAND                  CREATED       STATUS       PORTS                                           NAMES
37c8bc5ba6ea   mongo:5.0   "docker-entrypoint.s…"   4 hours ago   Up 4 hours   0.0.0.0:27017->27017/tcp, :::27017->27017/tcp   my-mongodb

In my case the container is running as my-mongodb the name we gave it. We can login to the container using the docker exec command while executing /bin/bash interactively. Here we are also logging in to posgtres with the credentials we specified above and checking the version.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
$ docker exec -it my-mongodb /bin/sh

# mongo
MongoDB shell version v5.0.3
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("c45a03a7-55a8-490d-aceb-f7b25fc75f57") }
MongoDB server version: 5.0.3
================
Warning: the "mongo" shell has been superseded by "mongosh",
which delivers improved usability and compatibility.The "mongo" shell has been deprecated and will be removed in
an upcoming release.
We recommend you begin using "mongosh".
For installation instructions, see
https://docs.mongodb.com/mongodb-shell/install/
================
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
	https://docs.mongodb.com/
Questions? Try the MongoDB Developer Community Forums
	https://community.mongodb.com
> db.version()
5.0.3

If you need to clean up the container when not in use, you can stop and remove the container using this command:

1
2
3
4
docker stop my-mongodb

# Removing
docker rm my-mongodb

# Using the docker-compose tool

We can achieve the same functionality with docker-compose. Docker Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a YAML file to configure your application’s services.

Docker Compose allows you to define the service (Mongodb in our case) with properties like the image to use, command to overide the default Dockerfile command and andports to expose.

Here is how we would use docker-compose to achieve the functionality above. Save this as docker-compose.yaml:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
version: "3.9"

services:
  mongodb:
    image: mongo:5.0
    ports:
      - 27017:27017
    volumes:
      - ~/apps/mongo:/data/db
    environment:
      - MONGO_INITDB_ROOT_USERNAME=citizix
      - MONGO_INITDB_ROOT_PASSWORD=S3cret

Now bring up the containers:

1
2
$ docker-compose up -d
Creating tmp_mongodb_1 ... done

The commands:

  • up brings up the container
  • -d in a detached mode

Verify the container processes using the ps command:

1
2
3
4
$ docker-compose ps
    Name                  Command             State                      Ports
--------------------------------------------------------------------------------------------------
tmp_mongodb_1   docker-entrypoint.sh mongod   Up      0.0.0.0:27017->27017/tcp,:::27017->27017/tcp

To login to the container and login to mongodb, use this:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
$ docker-compose exec mongodb /bin/sh
# mongo
MongoDB shell version v5.0.3
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("f481e3fd-1b45-480b-b43e-65686d3e2935") }
MongoDB server version: 5.0.3
================
Warning: the "mongo" shell has been superseded by "mongosh",
which delivers improved usability and compatibility.The "mongo" shell has been deprecated and will be removed in
an upcoming release.
We recommend you begin using "mongosh".
For installation instructions, see
https://docs.mongodb.com/mongodb-shell/install/
================
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
	https://docs.mongodb.com/
Questions? Try the MongoDB Developer Community Forums
	https://community.mongodb.com
> db.version()
5.0.3
>

# Conclusion

In this guide we managed to run Mongodb 5 as a container in our system, we explored using the docker run command while passing the required arguments an alternative approach of simplifying the process with docker-compose

comments powered by Disqus
Citizix Ltd
Built with Hugo
Theme Stack designed by Jimmy