Sunday, November 30, 2014

Jump in to the world of containers with docker

Why we need containers?

If we consider SDLC there are number of parties involve in different activities on our application.

Eg :- Developers, Testers, SysAdmins etc

There are various problems which these people need to address, because of platform and dependency standardization problems.

Docker address this problem with an open platform which "Build, Ship and Run Any App, Anywhere". Why docker VM's have already solved this problem? yes but with a virtual machine we are shipping our application+dependencies (assume 200MB) + guest OS (> 1GB) ouch :( that is painful.

Compared to VM, docker container includes only our application+dependencies :) Docker engine runs on top of the host OS and it's portable.

Let's jump in to our container, in this sample I'm going to create a docker container to ship WSO2 Enterprise store.

udara@udara-home:~$ sudo docker images
[sudo] password for udara:
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
ubuntu              latest              86ce37374f40        4 days ago          192.7 MB


I have ubuntu docker image with me and I'm going to start it.
sudo docker run -i -t ubuntu /bin/bash

With the above docker command I can login to the terminal of our newly created node,

udara@udara-home:~$ sudo docker run -i -t ubuntu /bin/bash
root@df8485e65e93:/#


Before running Enterprise Store inside our container I need to follow installation guidelines available here. So in this scenario we need to install JDK within our container.

Run following commands within your container terminal to install Java, zip and unzip.
apt-get install software-properties-common
add-apt-repository ppa:webupd8team/java
apt-get update
apt-get install oracle-java7-installer zip unzip

During this process if get "apt-get can't find the package" error similar to following as in my case use following command to lists to use old-releases.ubuntu.com,
sudo sed -i -e 's/archive.ubuntu.com\|security.ubuntu.com/old-releases.ubuntu.com/g' /etc/apt/sources.list

Err http://archive.ubuntu.com/ubuntu/ quantal/main openssl amd64 1.0.1c-3ubuntu2
  404  Not Found [IP: 91.189.91.14 80]
Err http://archive.ubuntu.com/ubuntu/ quantal/main ca-certificates all 20120623
  404  Not Found [IP: 91.189.91.14 80]
Err http://archive.ubuntu.com/ubuntu/ quantal/main libgirepository-1.0-1 amd64 1.33.14-1
  404  Not Found [IP: 91.189.91.14 80]
Err http://archive.ubuntu.com/ubuntu/ quantal/main gir1.2-glib-2.0 amd64 1.33.14-1
  404  Not Found [IP: 91.189.91.14 80]
Err http://archive.ubuntu.com/ubuntu/ quantal/main iso-codes all 3.38-1
  404  Not Found [IP: 91.189.91.14 80]
Err http://archive.ubuntu.com/ubuntu/ quantal/main python-apt-common all 0.8.7ubuntu4
  404  Not Found [IP: 91.189.91.14 80]
Err http://archive.ubuntu.com/ubuntu/ quantal/main python3-apt amd64 0.8.7ubuntu4
  404  Not Found [IP: 91.189.91.14 80]
Err http://archive.ubuntu.com/ubuntu/ quantal/main python3-dbus amd64 1.1.1-1
  404  Not Found [IP: 91.189.91.14 80]
Err http://archive.ubuntu.com/ubuntu/ quantal/main python3-gi amd64 3.4.0-1
  404  Not Found [IP: 91.189.91.14 80]
Err http://archive.ubuntu.com/ubuntu/ quantal/main unattended-upgrades all 0.79.3ubuntu4
  404  Not Found [IP: 91.189.91.14 80]
Err http://archive.ubuntu.com/ubuntu/ quantal/main python3-software-properties all 0.92.9
  404  Not Found [IP: 91.189.91.14 80]
Err http://archive.ubuntu.com/ubuntu/ quantal/main software-properties-common all 0.92.9

Set JAVA_HOME property using,
export JAVA_HOME=/usr/lib/jvm/java-7-oracle

Since Java is the only dependency to start any WSO2 product, I'm going to save the container status so I can create a container with any other product in future without installing Java.

Before Creating a new image from the above changes, we need to get our container ID,

How to list existing containers ID?

udara@udara-home:~$ sudo docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
df8485e65e93        ubuntu:latest       "/bin/bash"         6 minutes ago       Up 6 minutes                            compassionate_elion  
10a4800560de        ubuntu:latest       "/bin/bash"         27 minutes ago      Up 27 minutes                           dreamy_nobel


Let's find out changes we have done to the existing container by running,
sudo docker diff <container_id>
Eg :- sudo docker diff e69ccb432919

Ok, now I'm going to create new docker image by providing name "udara/java7"
sudo docker commit e69ccb432919 udara/java7

Our new image "udara/java7" is listed if I run command sudo docker images

udara@udara-home:~$ sudo docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
udara/java7         latest              76f948ba0aff        24 seconds ago      762.3 MB
ubuntu              latest              86ce37374f40        4 days ago          192.7 MB
base                latest              b750fe79269d        20 months ago       175.3 MB
base                ubuntu-12.10        b750fe79269d        20 months ago       175.3 MB
base                ubuntu-quantal      b750fe79269d        20 months ago       175.3 MB
base                ubuntu-quantl       b750fe79269d        20 months ago       175.3 MB


Lets start Enterprise Store instance within our initial docker image,

Before that we need to copy Enterprise Store distribution from our host OS to docker image. You need to run following commands within your host OS.

Run following command to get the full container ID
sudo docker inspect -f  '{{.Id}}'<container_id>
Eg:- sudo docker inspect -f  '{{.Id}}' e69ccb432919

This command will return the full container ID, in my case its,

udara@udara-home:~$ sudo docker inspect -f   '{{.Id}}' e69ccb432919
e69ccb432919f7432e1cf804693662a9adc61fc5a10f16d1a87b0ff7993b1254


Let's copy ES to the docker image using following command.
sudo cp wso2store-1.0.0.zip /var/lib/docker/aufs/mnt/<full_container_id>/tmp/

Eg:-

sudo cp wso2store-1.0.0.zip /var/lib/docker/aufs/mnt/e69ccb432919f7432e1cf804693662a9adc61fc5a10f16d1a87b0ff7993b1254/tmp/

I copied wso2store-1.0.0.zip in to the /tmp directory of my docker instance. Go into the /tmp directory an unzip ES distribution. Change directory to wso2store-1.0.0/bin and start ES instance using sh wso2server.sh

Now let's create a separate Docker image consists of ES distribution which we can share among core developers/QA team. They can just start ES instance without worrying about underlying dependencies. (this example only Java but in a real world project there can be thousands of dependencies)

If you are new to docker follow this try-it tool available on your browser. If you want to try docker on your local machine here is the link to installation.Docker is available on almost all environments.

No comments:

Post a Comment