Mongo on Docker

Robert Fehrmann
Snagajob Engineering
3 min readNov 12, 2014

Last night I presented at our bi-monthly meeting for the Richmond Mongo Users Group on how to build a fully functional replica set using docker containers. Docker has recently gotten a lot of traction so here’s a little how-to on how to setup dockerized replica set in Digital Ocean.

So what is Docker? “Docker is an open platform for developers and sysadmins to build, ship, and run distributed applications. …” (more details here). In essence it’s a new approach to visualization. Docker containers are similar to virtual machines in the sense that a Docker container is running it’s own full copy of an operating systems (guest) (i.e. any Linux flavor) . The Docker daemon (the service running the docker container) is running on a docker host machine. The host is also running any of the major Linux flavors. A Docker container is defined by a dockerfile (kind of definition for the virtual machine). You can either create and build your own dockerfiles, or you can take advantage of already existing images.

For this exercise we will run Ubuntu as the host OS as well as Ubuntu as the guest OS. To do so we will provision an Ubuntu virtual machine in Digital Ocean, install docker, instantiate the existing dockerfile/mongodb 3 times, and then configure the replica set.

Here’s a link to the instructions for installing docker. All you need to do is to run the following command.

curl -sSL https://get.docker.com/ubuntu/ | sudo sh

The next step is to setup the directories the 3 mongo dockerized containers will use to store their data. The naming convention here is that we call the replica set mug-3000 and append 1/2/3 to this name for each of the instances.

mkdir /db 
mkdir /db/mug-3000
mkdir /db/mug-3000/mug-30001
mkdir /db/mug-3000/mug-30002
mkdir /db/mug-3000/mug-30003

Now we can start the 3 docker containers via the following commands. The -p parameter maps the internal (to the container) network port of 27017 to the external port (30001/2/3) and the -v parameter maps the internal (to the container) path /data/db to the external path.

docker run -d -p 30001:27017 -v /db/mug-3000/mug-30001:/data/db — name mug-30001 dockerfile/mongodb mongod — smallfiles — replSet mug-3000 
docker run -d -p 30002:27017 -v /db/mug-3000/mug-30002:/data/db — name mug-30002 dockerfile/mongodb mongod — smallfiles — replSet mug-3000
docker run -d -p 30003:27017 -v /db/mug-3000/mug-30003:/data/db — name mug-30003 dockerfile/mongodb mongod — smallfiles — replSet mug-3000

Now you should be able to connect to the replica set via the docker hosts IP and port 30001. If you don’t have access to a mongo client you can just start another mongo container in interactive mode.

docker run -it — rm — name mug-3000-client dockerfile/mongodb /bin/bash

After the container starts you can start the mongo client via (replace <host-ip> with the actual IP of the docker host)

mongo <host-ip>:30001

The last step is to initiate and reconfigure the replica set. Be sure to replace <host-ip> with the actual address.

rs.initiate(); 
x= { “_id” : “mug-3000”, “version” : 2, “members” : [ { “_id” : 0, “host” : “<host-ip>:30001” }, { “_id” : 1, “host” : “<host-ip>:30002” }, { “_id” : 2, “host” : “<host-ip>:30003” } ] } rs.reconfig(x);

For reference, here’s a link to the presentation.

Mongo — Docker ( Meetup 11_11_2014)

Enjoy and let me know if you have any questions.

Originally published at engineering.snagajob.com on November 12, 2014.

--

--