Production Quality MongoDB Setup with Docker

Docker is an amazing piece of technology. If you’ve not try that yet, you should. Start with this getting started guide.

We started to use docker at MeteorHacks recently and there was nothing on the internet which shows us how to run a mongodb in production with docker. There is a guide on the docs site, but it is a just a mongo docker image. (It is good as a getting started material)

This is what I meant by production quality

  • data should be seperated from the docker container
  • we need to be able to specify an custom config file
  • we should be able to do admin tasks without installing mongo to the host

Before going into further, we need to understand two good features of docker. Which are,

Docker Volumes

With docker it is possible to mount an directory in the host system to the docker container at the runtime.

That means, you can share a directory in your host machine with the docker container.

Any change happens to the mounted volume is not considered as a change made to the container itself.

Here is a sample syntax to mount /home/data folder to a docker container as /data

docker run -v /home/data:/data ubuntu bash

Port Forwarding

docker has a simple and easy support for port forwarding. You can do this via a Dockerfile or when you starting the container. I prefer second option, which gives us a lot of flexibility.

Here is the sample synatx to expose containers port 27017 as 37017

docker run -p 37017:27017 ubuntu bash

Planning for the Docker Image

We need to follow this setup (at least in this tutorial)

  • We’ll mount directory(arunoda/data) in host to the docker container as /data
  • It has db directory which will be used as the location for the db data
  • It has the mongodb.conf file with db path specified as /data/db
  • We’ll be running mongodb in the default port (27017)


#MongoDB Image for MeteorHacks
FROM ubuntu
MAINTAINER Arunoda Susiripala,
RUN apt-key adv —keyserver hkp:// —recv 7F0CEB10
RUN echo ‘deb dist 10gen’ | tee /etc/apt/sources.list.d/10gen.list
RUN apt-get update -y
RUN apt-get install mongodb-10gen=2.4.5
ENTRYPOINT [“mongod”, “-f”, “/data/mongodb.conf”]

(use below link to get the code, don’t copy code above)

Building the Image

docker build -t mh/mongo .

Starting the Container

We’ll start the container as a daemon and container id will be assigned to CID. We can store this CID in some place, which can be restored later.

CID=$(docker run -d -v /arunoda/data:/data -p 5000:27017 mh/mongo)

Now our mongodb instance is running successfully. It will be exposed as port 5000.


Docker has a built in command to get logs from the container. We can use following command to get logs.

docker logs $CID

But I would recommend to use a custom log file location specified in the mongodb.conf

Admin Tasks

Okay, now we need to do some admin operations against our mongodb instance.

Do we need to install mongodb in the host system? Hell no.

Look into these docker concepts first


Our docker image has a default ENTRYPOINT. So we can’t access a mongo repl from it. But we can override the ENTRYPOINT at run time.

See following example, I change entrypoint as mongo —help

docker run -entrypoint “mongo —help” mh/mongo

IP Address

Each docker container has its own IP Address. We can see it using the following command.

docker inspect $CID

It’ll show a JSON object with a lot of configurations. You can clearly see the IP Address there.

If you like to get the IP Address directly you can use following command

IP_ADDRESS=$(echo `docker inspect $CID` | python -c ‘import json,sys;obj=json.load(sys.stdin);print obj[0][“NetworkSettings”][“IPAddress”]’)

Let’s start the admin container

Now we are ready to start a mongodb docker container which will be used to do the admin operations.

docker run -i -t -entrypoint “mongo” mh/mongo $IP_ADDRESS

Now you have a mongodb repl and you can do anything you want :)

Show your support

Clapping shows how much you appreciated Arunoda Susiripala’s story.