Running Web Application in Linked Docker Containers Environment

In post Dockerizing Web Application with Puppet we hosted web application in a single container, this time we will host a web application in a linked docker container environment – one in which our database (mysql) resides leveraged by our web application hosted in another docker container.

Before start, let’s have a brief about Linking Docker containers and how it helps us.

Linking or connecting Docker containers?
Linking Docker containers allows containers to discover each other and securely transfer information between them. Linking set up a conduit between containers allowing recipient container securely access source container preventing exposing the source container to the network.

In this post, recipient container is the spring-application-container which we created in this post and source container is the database container which we create now.

Let’s start with creating database container and linking it with  spring-application-container, following below steps:

Step 1: Create directory with any name for me it’s database-container inside directory docker (created in this post), as follows:

$ cd docker
$ mkdir database-container
$ cd database-container
$ touch Dockerfile

Step 2: Copy the below content in docker/database-container/Dockerfile:

FROM ubuntu:latest
MAINTAINER arpitaggarwal "aggarwalarpit.89@gmail.com"
RUN apt-get install -q -y mysql-server
RUN apt-get install -q -y mysql-client
RUN sed -i -e"s/^bind-address\s*=\s*127.0.0.1/bind-address = 0.0.0.0/" /etc/mysql/my.cnf
EXPOSE 3306

RUN sed -i -e”s/^bind-address\s*=\s*127.0.0.1/bind-address = 0.0.0.0/” /etc/mysql/my.cnf specified above is to set the MYSQL bind-address to 0.0.0.0 because it usually only listens on 127.0.0.1 by default.

Step 3: Build the newly created database-container as follows:

$ cd database-container
$ docker build --no-cache=true -t database .

database specified above refers to name of a database-container image.

Step 4: Start database-container assigning a name “db” and MYSQL Server installed as a service inside database-container, as follows:

$ docker run -P -it --name db database /bin/bash

Step 5: Modify the existing spring-application-container Dockerfile to copy the new application to the container which is using database hosted on database-container, as follows:

FROM ubuntu:latest
MAINTAINER arpitaggarwal "aggarwalarpit.89@gmail.com"
RUN apt-get -y update
RUN apt-get -q -y install git
RUN sudo apt-get install -y ruby
RUN apt-get install -y ruby-dev
RUN apt-get -y update
RUN apt-get install -y make
RUN apt-get install -y build-essential
RUN apt-get install -y puppet
RUN gem install librarian-puppet
ADD Puppetfile /
RUN librarian-puppet install
RUN puppet apply --modulepath=/modules -e "include java8 class { 'tomcat':version => '7',java_home => '/usr/lib/jvm/java-8-oracle'}"
RUN apt-get remove -y make puppet build-essential ruby-dev
COPY /spring-mysql/target/spring-mysql.war /var/lib/tomcat7/webapps/
EXPOSE 8080

Step 6: Build the the application inside a docker directory, this time spring-mysql cloned from github:


$ cd docker
$ git clone https://github.com/arpitaggarwal/spring-mysql.git
$ cd spring-mysql
$ mvn clean install

Step 7: Next, start spring-application-container linking it with database-container as follows:

$ docker run -p 8080:8080 -it --name webapp --link db spring-application-container /bin/bash

–link flag specified above create a secure link between spring-application-container with the database-container and exposes connectivity information for the source container to the recipient container in two ways:

a). Environment variables.
b). Updating the /etc/hosts file.

Now we can use exposed environment variables or the entries from host to access the db container. Also, If we restart the source container, the linked containers /etc/hosts files will be automatically updated with the source container’s new IP address, allowing linked communication to continue.

In our application, we used the host entry mechanism to read the IP address of source container, using Java InetAddress.

Step 8: Our application will try to access the mysql database with user as “test” and password as “test” and use the employee table to store the employee details submitted from applicationso let’s create it:

$ mysql --user=root mysql
$ CREATE USER 'test'@'%' IDENTIFIED BY 'test’;
$ GRANT ALL PRIVILEGES ON *.* TO 'test'@'%' WITH GRANT OPTION;
$ FLUSH PRIVILEGES;
$ CREATE DATABASE  test;
$ USE TEST;
$ CREATE TABLE employee (id INT NOT NULL PRIMARY KEY AUTO_INCREMENT, name VARCHAR(20), age VARCHAR(30));

Step 9: Get your Docker container IP Address, using docker-machine:

docker-machine ip your_vm_name

Next, create employee submitting name and age from application and refresh the screen to retrieve it from database at url http://container-ip-address:8080/spring-mysql

The complete source code is hosted on github.

 

Advertisements

Dockerizing Web Application with Puppet

In post Installing Puppet Modules – Librarian Puppet we provisioned a Vagrant VM to install the puppet module from  Puppet Forge using librarian-puppet, now in this post we will do the same thing except we provision Docker container instead of Vagrant VM and deploy a hello-spring application in provisioned container.

Before starting with application deployment in Docker container let’s understand brief about Docker and how it is different from Virtual Machine.

What is Docker?

Docker is a high level abstraction over linux containers which manages the life cycle of containers. Docker allows to package an application with all of its dependencies into a standardize unit for software development. And if that unit runs on your local, we can guarantee that it will run exactly the same way, anywhere from QA, to staging, to production environments.

Docker vs Virtual Machine

Virtualization technologies like VirtualBox, VMWare, KVM, etc. use full machine virtualization whereas Docker share certain portions of the host kernel and operating system instance offering lower overhead at the cost of less isolation. Also, Docker provides a virtual environment that has its own CPU, memory, block I/O, network, etc. space using Cgroups features in Linux kernel on LXC host compared to virtualization technologies which offer us complete new virtual machine.
Now, let’s start with our deployment in container, following below steps:

Step 1: Create a directory with any name for me it’s docker and file with name Dockerfile and  Puppetfile inside the directory as follows:


$ mkdir docker
$ cd docker
$ touch Dockerfile Puppetfile

Dockerfile created above contains all the commands a user could call on the command line to assemble an image.

Puppetfile created above is a Ruby-based DSL specifies where puppet modules should be installed and where to fetch the modules from.

Step 2: Copy the below content in Puppetfile:

# Puppetfile Configuration for librarian-puppet.
forge "http://forge.puppetlabs.com"
mod "puppet-java8", :git => "https://github.com/arpitaggarwal/puppet-java8.git"
mod "arpitaggarwal/tomcat"

Step 3: Copy the below content in Dockerfile

FROM ubuntu:latest
MAINTAINER arpitaggarwal "aggarwalarpit.89@gmail.com"
RUN apt-get -y update
RUN apt-get -q -y install git
RUN sudo apt-get install -y ruby
RUN apt-get install -y ruby-dev
RUN apt-get -y update
RUN apt-get install -y make
RUN apt-get install -y build-essential
RUN apt-get install -y puppet
RUN gem install librarian-puppet
ADD Puppetfile /
RUN librarian-puppet install
RUN puppet apply --modulepath=/modules -e "include java8 class { 'tomcat':version => '7',java_home => '/usr/lib/jvm/java-8-oracle'}"
RUN apt-get remove -y make puppet build-essential ruby-dev

Step 4: Build the the application inside a docker directory, for me it’s hello-spring cloned from github:

$ git clone https://github.com/arpitaggarwal/hello-spring.git
$ cd hello-spring
$ mvn clean install

Step 5: Update the Dockerfile adding your .war file location using Docker COPY command:

COPY hello-spring/target/hello-spring.war /var/lib/tomcat7/webapps/
EXPOSE 8080

EXPOSE command specified above refers to Docker command which is good for inter-container communication.

Finally, Dockerfile should like as:

FROM ubuntu:latest
MAINTAINER arpitaggarwal "aggarwalarpit.89@gmail.com"
RUN apt-get -y update
RUN apt-get -q -y install git
RUN sudo apt-get install -y ruby
RUN apt-get install -y ruby-dev
RUN apt-get -y update
RUN apt-get install -y make
RUN apt-get install -y build-essential
RUN apt-get install -y puppet
RUN gem install librarian-puppet
ADD Puppetfile /
RUN librarian-puppet install
RUN puppet apply --modulepath=/modules -e "include java8 class { 'tomcat':version => '7',java_home => '/usr/lib/jvm/java-8-oracle'}"
RUN apt-get remove -y make puppet build-essential ruby-dev
COPY hello-spring/target/hello-spring.war /var/lib/tomcat7/webapps/
EXPOSE 8080

Step 6: Build the Docker container using Docker build command:


docker build --no-cache=true -t spring-application-container .

spring-application-container specified above refers to name of a docker image.

Step 7: Once built, run the container using Docker run command:

docker run -p 8080:8080 --cap-add SYS_PTRACE -it spring-application-container /bin/bash

Step 8: Get your Docker container IP Address, using docker-machine:


docker-machine ip your_vm_name

Next, open the url http://container-ip-address:8080/hello-spring

The complete source code is hosted on github.