Quantcast
Channel: Ramblings on life & code » James Tan
Viewing all articles
Browse latest Browse all 10

MongoDB & Docker – Part 1

$
0
0

Docker

Docker is rapidly gaining popularity as it makes lightweight Linux containers easy to use, among other things. If you’re new to Docker, wondering how it is different from virtual machines and what benefits it provides, check this out first.

This blog post series will walk through step-by-step how to best use MongoDB on Docker, from simple single-process development/test setups to more complex multi-host production replica set setups, with thoughts on log management, performance, data persistence, etc.

Virtual Machines (VMs) vs Docker containers.

Virtual Machines (VMs) vs Docker containers.

 

There are plenty of online resources out there already talking about MongoDB on Docker, but most seem to be based on Ubuntu and describe only single host setups that are not ideal for production usage (e.g. docs, this and that blog post). Nothing against Ubuntu (or Debian), but I figured it will be useful to have some examples with RPM-based Linux distros such as Fedora/RHEL and openSUSE/SLES. So let’s begin with a simple Dockerfile based on the latest version of CentOS (version 7 at this time of writing):

# mongod process on latest CentOS
# See https://docs.docker.com/articles/dockerfile_best-practices/

FROM centos                             # Base container on latest CentOS release
MAINTAINER James Tan <james.tan@mongodb.com>

COPY mongodb.repo /etc/yum.repos.d/     # Copy yum repo config file to add MongoDB repo
RUN yum install -y mongodb-org-server   # Install mongod
RUN mkdir -p /data/db                   # Create default dbpath

ENTRYPOINT [ "mongod" ]                 # Execute the mongod binary on container startup

Then create the following file mongodb.repo in the same directory:

[mongodb]
name=MongoDB Repository
baseurl=http://downloads-distro.mongodb.org/repo/redhat/os/x86_64/
gpgcheck=0
enabled=1

We can now build our Docker image by running docker build -t mongod . in the terminal, with the current directory containing the two files above. The first run will take a while as the dependencies (e.g. CentOS, MongoDB) are downloaded and installed. Subsequent runs are fast as each command/stage is cached; thus if nothing changed it is basically instant. For example:

$ ls -l
-rw-r--r--. 1 vagrant vagrant 283 Jan 13 22:27 Dockerfile
-rw-r--r--. 1 vagrant vagrant 122 Jan  5 20:21 mongodb.repo

$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
centos              latest              8efe422e6104        8 days ago          224 MB

$ docker build -t mongod .
Sending build context to Docker daemon 3.072 kB
Sending build context to Docker daemon
Step 0 : FROM centos
 ---> 8efe422e6104
Step 1 : MAINTAINER James Tan <james.tan@mongodb.com>
 ---> Running in 4e5110a62897
 ---> 299f6b443463
Removing intermediate container 4e5110a62897
Step 2 : COPY mongodb.repo /etc/yum.repos.d/
 ---> 93e9e1c7080c
Removing intermediate container 365f1f6a898e
Step 3 : RUN yum install -y mongodb-org-server
 ---> Running in 6139c81c9b03
Loaded plugins: fastestmirror
Determining fastest mirrors
 * base: ftp-stud.fht-esslingen.de
 * extras: mirror.euserv.net
 * updates: ftp-stud.fht-esslingen.de
Resolving Dependencies
--> Running transaction check
---> Package mongodb-org-server.x86_64 0:2.6.6-1 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

================================================================================
 Package                   Arch          Version           Repository      Size
================================================================================
Installing:
 mongodb-org-server        x86_64        2.6.6-1           mongodb        9.0 M

Transaction Summary
================================================================================
Install  1 Package

Total download size: 9.0 M
Installed size: 23 M
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : mongodb-org-server-2.6.6-1.x86_64                            1/1
  Verifying  : mongodb-org-server-2.6.6-1.x86_64                            1/1

Installed:
  mongodb-org-server.x86_64 0:2.6.6-1

Complete!
 ---> 1c66942e38f2
Removing intermediate container 6139c81c9b03
Step 4 : RUN mkdir -p /data/db
 ---> Running in a9eaa1e46fde
 ---> 74b32e948366
Removing intermediate container a9eaa1e46fde
Step 5 : ENTRYPOINT mongod
 ---> Running in fbc8e63727b0
 ---> d491f0032d89
Removing intermediate container fbc8e63727b0
Successfully built d491f0032d89

Run docker images to see that there is now a “mongod” image/repository. For example:

$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED              VIRTUAL SIZE
mongod              latest              d491f0032d89        About a minute ago   330.3 MB
centos              latest              8efe422e6104        8 days ago           224 MB

Start the freshly built image by running docker run -d -P mongod --smallfiles. Run docker ps to see currently running containers and the mapped ports. For example:

$ docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

$ docker run -d -P mongod --smallfiles
f81a716e1f77a0b0ab1e3d6ca0e20b084897c0591bcdef9df4a6a927a9b5a1d2

$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                      NAMES
f81a716e1f77        mongod:latest       "mongod"            6 seconds ago       Up 5 seconds        0.0.0.0:49155->27017/tcp   sharp_goodall

View the logs (e.g. STDOUT) of the container by running docker logs CONTAINER_ID. For example:

$ docker logs f81a716e1f77
mongod --help for help and startup options
2015-01-13T23:21:12.876+0000 [initandlisten] MongoDB starting : pid=1 port=27017 dbpath=/data/db 64-bit host=f81a716e1f77
2015-01-13T23:21:12.877+0000 [initandlisten] db version v2.6.6
2015-01-13T23:21:12.877+0000 [initandlisten] git version: 608e8bc319627693b04cc7da29ecc300a5f45a1f
2015-01-13T23:21:12.877+0000 [initandlisten] build info: Linux build10.nj1.10gen.cc 2.6.32-431.3.1.el6.x86_64 #1 SMP Fri Jan 3 21:39:27 UTC 2014 x86_64 BOOST_LIB_VERSION=1_49
2015-01-13T23:21:12.877+0000 [initandlisten] allocator: tcmalloc
2015-01-13T23:21:12.877+0000 [initandlisten] options: {}
2015-01-13T23:21:12.934+0000 [initandlisten] journal dir=/data/db/journal
2015-01-13T23:21:12.934+0000 [initandlisten] recover : no journal files present, no recovery needed
2015-01-13T23:21:13.516+0000 [initandlisten] preallocateIsFaster=true 7.7
2015-01-13T23:21:14.070+0000 [initandlisten] preallocateIsFaster=true 7
2015-01-13T23:21:15.577+0000 [initandlisten] preallocateIsFaster=true 5.8
2015-01-13T23:21:15.577+0000 [initandlisten] preallocating a journal file /data/db/journal/prealloc.0
2015-01-13T23:21:18.743+0000 [initandlisten] preallocating a journal file /data/db/journal/prealloc.1
2015-01-13T23:21:22.015+0000 [initandlisten] preallocating a journal file /data/db/journal/prealloc.2
2015-01-13T23:21:26.188+0000 [initandlisten] allocating new ns file /data/db/local.ns, filling with zeroes...
2015-01-13T23:21:26.278+0000 [FileAllocator] allocating new datafile /data/db/local.0, filling with zeroes...
2015-01-13T23:21:26.278+0000 [FileAllocator] creating directory /data/db/_tmp
2015-01-13T23:21:26.324+0000 [FileAllocator] done allocating datafile /data/db/local.0, size: 64MB,  took 0.01 secs
2015-01-13T23:21:26.329+0000 [initandlisten] build index on: local.startup_log properties: { v: 1, key: { _id: 1 }, name: "_id_", ns: "local.startup_log" }
2015-01-13T23:21:26.330+0000 [initandlisten] 	 added index to empty collection
2015-01-13T23:21:26.332+0000 [initandlisten] command local.$cmd command: create { create: "startup_log", size: 10485760, capped: true } ntoreturn:1 keyUpdates:0 numYields:0  reslen:37 147ms
2015-01-13T23:21:26.335+0000 [initandlisten] waiting for connections on port 27017

From the docker ps output, we can see that the “exposed” default mongod port 27017 in the container is mapped to port 49155 on the Docker host (which is localhost in my case as I’m on Linux and running Docker directly). Thus, I can connect to the mongod process in the container by running mongo --port 49155. For example:

$ mongo --port 49155
MongoDB shell version: 2.6.6
connecting to: 127.0.0.1:49155/test
>

That’s it for now. We’ll investigate the various Docker networking options and their trade-offs in the next post.


Viewing all articles
Browse latest Browse all 10

Trending Articles