Installing MongoDB With Docker

https://www.percona.com/blog/installing-mongodb-with-docker/

https://www.percona.com/blog/?p=67094

installing MongoDB with Docker

installing MongoDB with DockerFollowing the series of blogs written with the intention to describe basic operations matching Docker and open source databases, in this article, I will demonstrate how to proceed with installing MongoDB with Docker.

The first one, written by Peter Zaitsev, was Installing MySQL with Docker.

Before proceeding, it is important to double warn by quoting Peter’s article:

“The following instructions are designed to get a test instance running quickly and easily; you do not want to use these for production deployments”. 

Also, a full description of Docker is not the objective of this article, though I assume prior knowledge of Docker, and also you that already have it installed and configured to move on.

Docker quickly deploys standalone MongoDB containers. If you are looking for fast deployments of both replica sets and shards, I suggest looking at the mlaunch tool.

Peter mentioned, in his article, how MySQL has two different “official” repositories, and with MongoDB, it’s the same: MongoDB has one repository maintained by MongoDB and another maintained by Docker. I wrote this article based on the Docker-maintained repository.

Installing the Latest Version of MongoDB

The following snippet is one example of how to initiate a container of the latest MongoDB version from the Docker repository. 

docker run --name mongodb_dockerhub \
                -e MONGO_INITDB_ROOT_USERNAME=admin 
                -e MONGO_INITDB_ROOT_PASSWORD=secret \
                -d mongo:latest

Now, if you want to check the container status right after creating it:

docker ps

CONTAINER ID        IMAGE COMMAND                  CREATED STATUS PORTS               NAMES
fdef23c0f32a        mongo:latest "docker-entrypoint..."   4 seconds ago Up 4 seconds 27017/tcp           mongodb_dockerhub

Connecting to MongoDB Server Docker Container

Having the container installed up and running, you will notice that no extra step or dependency installation was previously required, apart from the docker binaries. Now, it is time to access the container MongoDB shell and issue a basic command like “show dbs”. 

docker exec -it mongodb_dockerhub mongo -u admin -p secret
MongoDB shell version v4.2.5
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("89c3fb4a-a8ed-4724-8363-09d65024e458") }
MongoDB server version: 4.2.5
Welcome to the MongoDB shell.

> show dbs
admin   0.000GB
config  0.000GB
local   0.000GB

> exit
bye

It is also possible to connect to the containerized MongoDB using a host mongo shell. On the docker ps output, the container id has a field that informs its port mapping, and then it is a simple connection using that port.

In the below example, we connected to the mongodb_dockerhub36. It’s up and running locally on port 27017  but mapped to the host 27018 port:

docker ps

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                      NAMES
60ffc759fab9        mongo:3.6           "docker-entrypoint..."   20 seconds ago      Up 19 seconds       0.0.0.0:27018->27017/tcp   mongodb_dockerhub36

Hence, the mongo shell connection string will be executed against the external IP and port 27018

mongo admin -u admin --host 172.16.0.10 --port 27018 -psecret

Managing MongoDB Server in Docker Container

The following commands will demonstrate basic management operations when managing a MongoDB container. 

  • Starting the container and checking the Status

docker start mongodb_dockerhub

docker ps

CONTAINER ID        IMAGE COMMAND                  CREATED STATUS PORTS               NAMES
fdef23c0f32a        mongo:latest "docker-entrypoint..."   47 minutes ago Up 2 seconds 27017/tcp           mongodb_dockerhub

  • Stopping the container and checking the Status

docker stop mongodb_dockerhub

docker ps

CONTAINER ID        IMAGE COMMAND

  • Taking a look at the MongoDB log entries

docker logs mongodb_dockerhub
...
about to fork child process, waiting until server is ready for connections.
forked process: 25
2020-04-10T14:04:49.808+0000 I  CONTROL [main] ***** SERVER RESTARTED *****
2020-04-10T14:04:49.812+0000 I  CONTROL [main] Automatically disabling TLS 1.0, to f
...

Passing Command Line Options to MongoDB Server in Docker Container

It is also possible to define the instance’s parameters when launching the MongoDB container. In the example below, I will show how to set WiredTiger cache.

docker run --name mongodb_dockerhub \
                -e MONGO_INITDB_ROOT_USERNAME=admin -e MONGO_INITDB_ROOT_PASSWORD=secret \
                -d mongo:latest --wiredTigerCacheSizeGB 1.0

Running Different MongoDB Server Versions in Docker

Another possibility is getting two MongoDB containers running in parallel but under different versions. The below snippet will describe how to build that scenario

  • Launching a 4.0 container

docker run --name mongodb_dockerhub40 \
 -e MONGO_INITDB_ROOT_USERNAME=admin -e MONGO_INITDB_ROOT_PASSWORD=secret \
 -d mongo:4.0

  • Launching a 3.6 container

docker run --name mongodb_dockerhub36 \
 -e MONGO_INITDB_ROOT_USERNAME=admin -e MONGO_INITDB_ROOT_PASSWORD=secret \
 -d mongo:3.6

  • Checking the container’s status

docker ps

CONTAINER ID        IMAGE COMMAND                  CREATED STATUS PORTS               NAMES
e9f480497f2d        mongo:3.6 "docker-entrypoint..."   32 seconds ago Up 32 seconds 27017/tcp           mongodb_dockerhub36
3a799f8a907c        mongo:4.0 "docker-entrypoint..."   41 seconds ago Up 41 seconds 27017/tcp           mongodb_dockerhub40

If for some reason you need both containers running simultaneously and access them externally, then use different port mappings. In the below example, both MongoDB’s containers are deployed on the local 27017 port, nevertheless, I am setting different external port maps for each one.

  • MongoDB 4.0 mapping the port 27017

docker run --name mongodb_dockerhub40 \
 -p 27017:27017 \
 -e MONGO_INITDB_ROOT_USERNAME=admin -e MONGO_INITDB_ROOT_PASSWORD=secret \
 -d mongo:4.0

  • MongoDB 3.6 mapping the port 27018

docker run --name mongodb_dockerhub36 \
 -p 27018:27017 \
 -e MONGO_INITDB_ROOT_USERNAME=admin -e MONGO_INITDB_ROOT_PASSWORD=secret \
 -d mongo:3.6

  • Checking both Container’s status

docker ps

CONTAINER ID        IMAGE COMMAND                  CREATED STATUS PORTS                      NAMES
78a79e5606ae        mongo:4.0 "docker-entrypoint..."   3 seconds ago Up 2 seconds 0.0.0.0:27017->27017/tcp   mongodb_dockerhub40
60ffc759fab9        mongo:3.6 "docker-entrypoint..."   20 seconds ago Up 19 seconds 0.0.0.0:27018->27017/tcp   mongodb_dockerhub36

There are a lot of extra details on Docker’s MongoDB Hub page and you will find more options to use on your MongoDB Docker deployment. Enjoy it!

Percona Distribution for MongoDB is a freely available MongoDB database alternative, giving you a single solution that combines the best and most important enterprise components from the open source community, designed and tested to work together.

Download Percona Distribution for MongoDB Today!