lundi 31 mars 2014

Build your own SaaS with Docker - Part I

Hello Docker
Docker enables sand-boxing of applications and their dependencies in virtual containers to be able to run them in isolated mode. It provides an easy to use API for automating deployment operations that looks very close to Git commands. More introductory information can be found in its Wikipedia page.

Installation
Docker installation on a Ubuntu 64bit (for other OS check official documentation)
$sudo sh -c "curl https://get.docker.io/gpg | apt-key add -" 
$sudo sh -c "echo deb http://get.docker.io/ubuntu docker main > /etc/apt/sources.list.d/docker.list" 
$sudo apt-get update
$sudo apt-get install lxc-docker

Once docker installed, run a shell from within a container as follow
$sudo docker run -i -t ubuntu /bin/bash

As it is supposed to not find the ubuntu image, docker will pull it from the registry. Once, installed you can prompt:
  • #exit to leave the container
  • $sudo docker images to see all local images.
  • $sudo docker inspect image_name to see detailed information on an image.
  • $sudo docker ps to see the status of the container
  • $sudo docker stop CONTAINER_ID to stop a running image (or container)
  • $sudo docker logs CONTAINER_ID to see all logs if a given container
  • $sudo docker commit CONTAINER_ID image_name to commit changes made to a container

Installing Tomcat within a container
Start a new container using the ubuntu base image:
$sudo docker run -i -t ubuntu /bin/bash

Update the image's system packages
#apt-get update

1. Install the Apache Tomcat application server:
#apt-get install -y tomcat7

Once installed the following directories are created (more details can be found here):
  • /etc/tomcat7 for configuration
  • /usr/share/tomcat7 for runtime, called $CATALINA_HOME
  • /usr/share/tomcat7-root for webapps
2. Install Java DK
#apt-get install -y default-jdk

3. Configure environment variables
#pico ~/.bashrc
export JAVA_HOME=/usr/lib/jvm/default-java
export CATALINA_HOME=~/path/to/tomcat
#. ~/.bashrc to make the changes effective

Now when typing #echo $CATALINA_HOME you should see the exact path set to tomcat7.

4. Start the Tomcat7 server
#$CATALINA_HOME/bin/startup.sh
or
#service tomcat7 start

The start-up may fail with something like "cannot create directory '/usr/share/tomcat7/logs/catalina.out/'". To solve this, you may just have create the logs directory:
#mkdir /usr/share/tomcat7/logs

to check if Tomcat is running issue
#ps -ef | grep tomcat
or
#service tomcat7 status

then check in your browser http://container_ip_address:8080/
to get the IP address of the container issue
#ifconfig

5. Shutdown  Tomcat7
#$CATALINA_HOME/bin/shutdown.sh
or
#service tomcat7 stop

Save the image to index.docker.io
The changes we made on the base image created a new one, we should commit these changes to not lose these changes.

1. Login to index.docker.io
$sudo docker login
Username: your_user_name
Password: your_password
Email: your_email
Login Succeeded

If you don't have an account, sign up here.

2. Commit changes to your repository
$sudo docker commit CONTAINER_ID USERNAME/REPO_NAME

3. Push changes to this repository
$sudo docker push USERNAME/REPO_NAME

4. Start a new container using the image commit to your repository as base image
$sudo docker run -i -t USERNAME/REPO_NAME /bin/bash
#

To run Tomcat in the container
$sudo docker run -i -t USERNAME/REPO_NAME $CATALINA_HOME/bin/startup.sh
or
$sudo docker run -i -t USERNAME/REPO_NAME service tomcat7 start

to cleanup old containers
$sudo docker ps -a -q | xargs sudo docker rm
or
$sudo docker ps -a | awk '{print $1}' | xargs sudo docker rm

to cleanup old and non tagged images
$sudo docker images | grep "^" | awk '{print $3}' | xargs sudo docker rmi -f

Resources
If you are confused with docker terminology (e.g. container, image, etc.) check this official documentation.
General purpose instructions for installing Tomcat7 on a ubuntu machine here.

2 commentaires:

  1. As per this page, i have understood that we need to create a image of base os and install tomcat and jdk then deploy the war file in webapp folder then commit the changes to the docker image and push it to the hub. Pull the container and run it to get the o/p. Is that right?

    RépondreSupprimer
  2. And Once i pulled one image, do i continue installing the tomcat and jdk normally or is there any other way that i need to do ? please reply

    RépondreSupprimer