Docker. First try

Recently I’ve read an article about docker. I became curious, why this new product became so popular in such short term. I can even say that it has become standard for web development.

As you may know, Docker is open source project for deployment automation. The main feature is docker containers – the container is  some kind of virtual machine with basic configuration, desired software and its dependencies. I will not try to explain all the magic that docker does, its website does it better. I started to help with the development of one nice start-up and decided to investigate, what docker could do for us.

I start all projects from DB architecture, this time is not an exception. After drawing some diagrams, I started to do the most boring task – writing SQL. I’m not only developer, so I decided somehow to share my configuration with others, so I pushed DDL to git.

Requirements are evaluating, something changes, I add tables, remove columns, change keys… A database is the lower level of a project, so every developer should have it up to date. The last straw was the decision to add MongoDB as the second database. Every developer should install and setup it. You know, developers hate installing software. Here the Docker appeared.

As starting point, I chose the sources of this pretty image

https://registry.hub.docker.com/u/macadmins/postgres/

It has opened ports by default and some DB evaluation scripts

Let’s look at the code of this basic Dockerfile.

FROM postgres #this means that current image configuration is based on image called 'postgres'

# we can introduce environment variables for installed system and
# even use it later in .sh scripts or pass through command line during docker execution
ENV DB_NAME noisedb
ENV DB_USER admin   
ENV DB_PASS password

#we also able to run sh scripts
ADD setupRemoteConnections.sh /docker-entrypoint-initdb.d/setupRemoteConnections.sh
RUN chmod 755 /docker-entrypoint-initdb.d/setupRemoteConnections.sh

All we need is to add mongo installation script. The task is trivial, but I’ve spent two hours to do it, the reason is that base image ‘postgres’ is based on Debian, but I’ve tried install it as it was Ubuntu.

Installing Mongo is just simple as write this:

# Install MongoDB.
RUN apt-key adv --keyserver keyserver.ubuntu.com --recv 7F0CEB10
RUN echo 'deb http://downloads-distro.mongodb.org/repo/debian-sysvinit dist 10gen' | tee /etc/apt/sources.list.d/mongodb.list
RUN apt-get update
RUN apt-get install -y mongodb-org-server
RUN sed 's/^bind_ip/#bind_ip/' -i /etc/mongod.conf
RUN rm -rf /var/lib/apt/lists/* # 20150323

#opening ports
EXPOSE 27017
EXPOSE 28017

To set up initial DB configuration, I put my script into separate file, and call it inside setup-database.sh

gosu postgres postgres --single -f noise_tables.sql

Full code you could find on my GitHub repo https://github.com/green-creeper/postmongo

Maybe it is not typical use case of Docker container, but it simplifies development very much. Will see, maybe I’ll able to use it for production purpose. The next thing I going to try with Docker – is REST API container for Scala