By Robert Hall
In the process of developing portals with Liferay, it is often necessary to set up and tear down servers with a Liferay instance on them. There are many ways of accomplishing this, such as using VMs, AWS EC2 instances, etc. One approach that has the promise of being simple, repeatable and lightweight is using new open source tools such as Vagrant and Docker to create a virtual application container. This post will explore a simple scenario--bringing up a Liferay server in a lightweight container on a virtual server instance.
What is Vagrant
Vagrant is an open-source tool for creating and configuring virtual development environments. It can be used to create Linux (various distributions such as Ubuntu and CentOS are supported) instances on a variety of hosting platforms, including Virtual Box (local to your computer) or cloud services such as AWS.
What is Docker
Docker is an open-source engine to automate the deployment of applications as self-contained, portable containers independent of hardware, language, framework, packaging system and hosting provider. Think of it as building a sandbox with a deployed, configured application inside.
Creating a Ubuntu VM with Vagrant
If you are already working on Linux, you can skip this section and go to ‘Creating a Docker container’. As I am working on a Mac, I have to set up a Linux--in this case, Ubuntu, environment with Vagrant before installing Docker as Docker presently only works on Linux. Note: In the example scenarios in this post, '%' or '#' denote a Linux command prompt.
1. create work folder such as '/home/user/dev/vagrant
2. create a Vagrant file. This example is for Ubuntu with Virutal Box. Other hosting providers such as AWS can be specified. Think of this as the configuration of your new virtual machine. The 'config.vm.forward_port' is of importance as this a directive to tell Vagrant to forward port 8080 as 7991. So when we run Liferay on the virtual server on port 8080, it will be accessible via the browser as localhost:7991.
Vagrant::Config.run do |config|
config.vm.box = "raring"
config.vm.box_url = "http://cloud-images.ubuntu.com/raring/current/raring-server-cloudimg-vagrant-amd64-disk1.box"
config.vm.forward_port 8080, 7991
config.vm.share_folder("vagrant-root", "/vagrant", ".")
config.vm.customize ["modifyvm", :id, "--memory", 2048
end
3. run vagrant up. This will instantiate a virtual machine instance using the Vagrantfile configuration.
% /Applications/Vagrant/bin/vagrant up
4. log into the instance
% /Applications/Vagrant/bin/vagrant ssh
Due to a bug in LXC (control package for Linux containers), Docker works best on the 3.8 kernel. Precise comes with a 3.2 kernel, so we need to upgrade it. The kernel we install comes with AUFS built-in. We also include the generic headers to enable packages that depend on them, like ZFS and the VirtualBox guest additions. If you didn’t install the headers for your “precise” kernel, then you can skip these headers for the “raring” kernel. But it is safer to include them if you’re not sure.5. Install the back-ported kernel
% sudo apt-get install linux-image-extra-$(uname -r) (answer 'y')
% sudo apt-get install linux-image-generic-lts-raring linux-headers-generic-lts-raring
6. install basic tools and common software
% sudo apt-get install vim curl git sudo zip
% sudo apt-get install software-properties-common
% sudo apt-get install python-software-properties
7. Update:
% sudo apt-get update
8. reboot
% sudo reboot
Use vagrant ssh to get back in.
Creating a Docker Container
Docker is available as a Ubuntu PPA (Personal Package Archive), hosted on launchpad which makes installing Docker on Ubuntu straightforward.
1. Add the PPA sources to your apt sources list.
% sudo add-apt-repository ppa:dotcloud/lxc-docker (ENTER to continue when prompted)
2. Update your sources
% sudo apt-get update
3. Install docker, you will see another warning that the package cannot be authenticated. Confirm install.
% sudo apt-get install lxc-docker (y to continue)
4. pull a base image
% sudo docker pull base
5. inspect images
% sudo docker images
6. start docker shell
% sudo docker run -i -t ubuntu /bin/bash
You will see a shell prompt such as the following:
root@209c4fc1eeaa:/#
Installing Java
In this container – which is a lightweight and sealed vm-like space isolated from the rest of your computer – we can install a JVM and its dependencies:
1. install some dependencies
# apt-get install software-properties-common (y to continue)
# apt-get install python-software-properties (y to continue)
# apt-get install sudo vim curl zip (y to continue)
2. create /etc/hosts with name 'dockerlr'. This will be useful later with Oracle and Liferay.
# echo '127.0.0.1 localhost 209c4fc1eeaa dockerlr' | cat > /etc/hosts
3. Add the ppa that will allow us to install Java:
# add-apt-repository ppa:webupd8team/java (ENTER to continue when prompted)
# apt-get update
4. Install Java:
# apt-get install software-properties-common -y apt-add-repository ppa:webupd8team/java -y
# apt-get update echo oracle-java7-installer shared/accepted-oracle-license-v1-1 select true |
sudo /usr/bin/debconf-set-selections
# apt-get install oracle-java7-installer -y
# exit
Get the process id of the instance you just exited
% sudo docker ps -a
209c4fc1eeaa ubuntu:12.04 /bin/bash 32 minutes ago Exit 0
Commit it, thus creating a reusable image:
% sudo docker commit 209c4fc1eeaa rhall/java7
Then, to see the images:
% sudo docker images
REPOSITORY TAG ID CREATED SIZE
base latest b750fe79269d 4 months ago 24.65 kB (virtual 180.1 MB)
base ubuntu-12.10 b750fe79269d 4 months ago 24.65 kB (virtual 180.1 MB)
base ubuntu-quantal b750fe79269d 4 months ago 24.65 kB (virtual 180.1 MB)
base ubuntu-quantl b750fe79269d 4 months ago 24.65 kB (virtual 180.1 MB)
ubuntu 12.04 8dbd9e392a96 4 months ago 131.5 MB (virtual 131.5 MB)
ubuntu 12.10 b750fe79269d 4 months ago 24.65 kB (virtual 180.1 MB)
ubuntu latest 8dbd9e392a96 4 months ago 131.5 MB (virtual 131.5 MB)
ubuntu precise 8dbd9e392a96 4 months ago 131.5 MB (virtual 131.5 MB)
ubuntu quantal b750fe79269d 4 months ago 24.65 kB (virtual 180.1 MB)
rhall/java7 latest 23143436f87b 55 seconds ago 472.9 MB (virtual 604.4 MB)
Installing Liferay CE
We now have a reusable Docker container consisting of Ubuntu with Java 7 installed. This is the minimum for installing Liferay CE. Spin up a Docker instance using the java7 image.
% sudo docker run -i -t -h dockerlr -p :8080 -v /opt/lr-home rhall/java7 /bin/bash
Install the latest LR CE---in this example, we use the CE (Community Edition) of Liferay as we don't have to deal with a license and the IP address/Mac address limitations of a production license.
# cd /opt/lr-home
Obtain the URL below from www.liferay.com->Downloads Liferay 6.1 CE and use wget to download the distribution.
# wget http://sourceforge.net/projects/lportal/files/Liferay%20Portal/6.1.1%20GA2/
liferay-portal-tomcat-6.1.1-ce-ga2-20120731132656558.zip
unzip it
# unzip *.zip
start Liferay
# sudo /opt/lr-home/liferay-portal-6.1.1-ce-ga2/tomcat-7.0.27/bin/startup.sh -fg
Setup liferay as a service
# sudo vi /etc/init.d/liferay
Place this in the script file called 'liferay':
# Liferay auto-start
# description: Auto-starts liferay
# processname: liferay
# pidfile: /var/run/liferay.pid
case $1 in
start)
sh /opt/lr-home/liferay-portal-6.1.1-ce-ga2/tomcat-7.0.27/bin/startup.sh
;;
stop)
sh /opt/lr-home/liferay-portal-6.1.1-ce-ga2/tomcat-7.0.27/bin/shutdown.sh
;;
restart)
sh /opt/lr-home/liferay-portal-6.1.1-ce-ga2/tomcat-7.0.27/bin/shutdown.sh
sh /opt/lr-home/liferay-portal-6.1.1-ce-ga2/tomcat-7.0.27/bin/startup.sh
;;
esac
exit 0
You will need to make the script executable by running the chmod command:
# sudo chmod 755 /etc/init.d/liferay
The last step is actually linking this script to the startup folders with a symbolic link. Execute these two commands and we should be on our way.
# sudo ln -s /etc/init.d/liferay /etc/rc1.d/K99liferay
# sudo ln -s /etc/init.d/liferay /etc/rc2.d/S99liferay
Liferay should now be fully installed and operational.
Running Liferay
To start Liferay from the command line,
# service liferay start
Screenshot on localhost:7991-initial
Liferay is now up in it’s most basic form using the classic theme and hsql. Time to configure an administrator user, email, and password and complete the basic installation.
Screenshot on localhost:7991 after configuration

To shut down Liferay,
# service liferay stop
Exit the instance
# exit
Check the recent processes
% sudo docker ps -a
ID IMAGE COMMAND CREATED STATUS PORTS
50b10bb82488 rhall/java7:latest /bin/bash 17 minutes ago Exit 0
209c4fc1eeaa ubuntu:12.04 /bin/bash 26 minutes ago Exit 0
Commit the latest update as it has installed and configured Liferay.
% sudo docker commit 50b10bb82488 rhall/liferayCEConfigured
Check that the new configuration is available as a named image:
% sudo docker images
Dockerfile
In a manner similar to a Vagrantfile, Docker supports scripting with a Dockerfile. This configuration can be built, comitted as an image, and run. For example, a Dockerfile for running Liferay could be:
FROM rhall/LiferayCEConfigured
MAINTAINER Robert Hall "robert.hall@isostech.com"
EXPOSE 8080
CMD service liferay start && tail -f /opt/lr-home/liferay-portal-6.1.1-ce-ga2/tomcat-7.0.27/logs/catalina.out
build and commit this Dockerfile
% sudo docker build -t rhall/LiferayCERun .
run it (wait 30-60 seconds before opening the browser on localhost:7991)
% sudo docker run -d -h dockerlr -p :8080 rhall/LiferayCERun
To kill it, issue
% sudo docker ps -a
to get the id. Then
% sudo docker kill ff59dd05989a
In this simple Liferay scenario, Liferay CE is using the default hsql (in-memory database) scenario. More complex configurations can be scripted in a Dockerfile---such as installation of a database server such as MySQL, setup of a user and database in MySQL, and configuration of connection properties in a portal-ext.properties file. Additionally, the install steps for Java and Liferay could be scripted as well.
Conclusions
Creating and provisioning working, isolated environments for application containers/servers such as Liferay, Tomcat or others can be simplified and standardized by using such virtualization tools as Vagrant and Docker. By using them together, you can quickly create a reusable, consistent operating environment for your application container.
References
Vagrant Documentation
Getting Started with Vagrant
Docker Site
Getting Started with Docker
Docker Command Line Interface
LXC Project