Build a simple Platform as a Service (PaaS) / Database as a Service (DBaaS) using Docker

Build a simple Platform as a Service (PaaS) / Database as a Service (DBaaS) using Docker

Docker is a Container based technology which enables us to create and manage linux containers in a efficient way. Docker is mostly used in automated deployments and managing workloads.

Docker can also be used to build PaaS applications.

In this tutorial I built a simple MongoDB Database as a Service application.

In the following example I copied the MongoDB files from local location to Docker file. These can be replaced by installation from Internet. If you are going to use the following scripts , you may need to download MongoDB locally and change appropriate path. 

############################################################################

Follow the steps to create MongoDB containers creation process

Admin task : (This is one time task) / Image creation

1) Create a folder (/root/admin_work/mongodb)
2) Create 2 files in this folder

File 1 name: Dockerfile
########START#########
############################################################
# Dockerfile to build MongoDB Images
# Base image : Ubuntu
############################################################

# Set the base image to Ubuntu
#FROM your base image
FROM ubuntu
#RUN apt-get update

# File Author / Maintainer
MAINTAINER Example Arun

#ADD <curr_dir/file> <DEST_LOC_IN_DOCKER>
ADD mongo_script /tmp/mongo_script

# Expose the default port of MongoDB
EXPOSE 27017

# Set default container command when container starts
CMD bash -C ‘/tmp/mongo_script’;’bash’

#########END###########

File 2 : mongo_script
########START#############
#/usr/bin/bash
# MongoDB by default needs this directory ‘/data/db’. The follwing two lines can also be replaced by single command ‘mkdir -p /data/db’
mkdir /data
mkdir /data/db
# Putting this script in sleep for 3 seconds so that the volumes(/tmp/db_files…) are mounted in Docker. This whole script runs in background (Ref: Dockerfile last line), hence no problem
sleep 3

/tmp/db_files/mongodb/mongodb-linux-x86_64-2.6.5/bin/mongod &

#########END#############

3) Now run this command from the directory where Dockerfile is located.

# docker build -t mongodb .

4) Now a new image called ‘mongodb’ will be builded. (‘#docker images’)

NOW YOU CAN LAUNCH ANY NUMBER OF MONGODB CONTAINERS

5)Now launch a container

# docker run -i -t -d -p 27017  -v /root/database_files:/tmp/db_files mongodb

6) That container will be running in the background.
See ‘#docker ps’ and ‘docker inspect <docker-id> | grep -i ipaddress’ to find the IP.

7) Now clients can connect to that mongodb server from a mongodb client.

I assume you are in /root directory. See using ‘pwd’.

Now connect to the mongodb server created using the docker IP.

#./database_files/mongodb/mongodb-linux-x86_64-2.6.5/bin/mongo 172.17.0.17

////Some basic mongo db commands – http://www.tutorialspoint.com/mongodb/mongodb_quick_guide.htm////
(create and use db)
>use test_db
(ENter data)
>db.test_db.insert({“name”:”tutorials point”})

8) Ok now clients can able to connect to mongodb container using that IP. But that is a internal IP, assigned from ‘docker0’ bridge (see using command ‘ip a’). Only from your machine that IP is accessible.
Good thing is we already exposed the port. (SEe using ‘docker ps’)
PORTS
0.0.0.0:49165->27017/tcp

It means that 172.17.0.17:27017 is equal to <YOUR-HOST-IP>:49165

This will be accessible from your friends laptop using LAN.
Ie MongoDB client can be copied, dont need to install. EXE file It is also available for Windows clients.

#./database_files/mongodb/mongodb-linux-x86_64-2.6.5/bin/mongo <YOUR-HOST-IP>:49165

########################################################################

By this I am able to create MongoDB containers, Now we have to build a GUI.

The code for GUI is available here https://github.com/bingoarunprasath/DBaaS

I used CAdvisor from google to monitor the containers. CAdvisor is available as a Docker container, we just need to run the container and from the port specified, we will be able to monitor the CPU, Memory load of the local containers.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s