Sunday, September 7, 2014

CentOS Docker Image with Tomcat 7

Start to experiment docker. However, most blog posts currently used example with Ubuntu and Tomcat. I am more interest in a combination of Fedora/CentOS/RHEL example with Tomcat 7. So I decide to do my own.

Instead of using a pre-build images, I decide I will experiment with building my own centos image.

Thanks for the contribution on GitHub from https://github.com/blalor/docker-centos-base

I modified it a little.

Following is the step I took (following steps do not assume any OS as base OS may not be centos as some people may want to produce one even without install on the hardware)

The overall summary is following:

Use Vagrant and VirtualBox to setup a minimum centos machine -> Use the minimum virtual machine to build a supermin Docker base image -> Use host to build docker image

Please download vagrant

https://www.vagrantup.com/

Please then download VirtualBox

https://www.virtualbox.org/

Installed both

Execute following
mkdir centos_docker_builder;

cd centos_docker_builder;

vagrant box add centos65-x86_64-20140116 https://github.com/2creatives/vagrant-centos/releases/download/v6.5.3/centos65-x86_64-20140116.box

cat << EOF > Vagrantfile
# -*- mode: ruby -*-
# vi: set ft=ruby :

# Vagrantfile API/syntax version. Don't touch unless you know what you're doing!
VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.box = "centos65-x86_64-20140116"
#  config.vm.provision "shell", path: "auto_build_setup.sh"
  config.vm.synced_folder ".", "/vagrant", :mount_options => ["dmode=777","fmode=666"]
end
EOF

cat << EOFF > start-tomcat.sh
#!/bin/bash

# From https://github.com/arcus-io/docker-tomcat7

ADMIN_USER=${ADMIN_USER:-admin}
ADMIN_PASSWORD=${ADMIN_PASSWORD:-admin}
MAX_UPLOAD_SIZE=${MAX_UPLOAD_SIZE:- 52428800}

cat << EOF > /opt/apache-tomcat/conf/tomcat-users.xml




EOF

if [ -f "/opt/apache-tomcat/webapps/manager/WEB-INF/web.xml" ];
then
   chmod 664 /opt/apache-tomcat/webapps/manager/WEB-INF/web.xml
   sed -i "s^.*max-file-size.*^\t${MAX_UPLOAD_SIZE}^g" /opt/apache-tomcat/webapps/manager/WEB-INF/web.xml
sed -i "s^.*max-request-size.*^\t${MAX_UPLOAD_SIZE}^g" /opt/apache-tomcat/webapps/manager/WEB-INF/web.xml
fi

/bin/sh -e /opt/apache-tomcat/bin/catalina.sh run
EOFF

wget http://www.carfab.com/apachesoftware/tomcat/tomcat-7/v7.0.55/bin/apache-tomcat-7.0.55.tar.gz

cat << EOF > build_centos.sh
#!/bin/bash

set -e
## Following script is coming from GitHub from https://github.com/blalor/docker-centos-base
## Thanks for the code

## requires running as root because filesystem package won't install otherwise,
## giving a cryptic error about /proc, cpio, and utime.  As a result, /tmp
## doesn't exist.
[ $( id -u ) -eq 0 ] || { echo "must be root"; exit 1; }

tmpdir=$( mktemp -d )
trap "echo removing ${tmpdir}; rm -rf ${tmpdir}" EXIT

febootstrap \
    -u http://mirrors.mit.edu/centos/6.5/updates/x86_64/ \
    -i centos-release \
    -i yum \
    -i iputils \
    -i tar \
    -i which \
    -i http://mirror.pnl.gov/epel/6/i386/epel-release-6-8.noarch.rpm \
    centos65 \
    ${tmpdir} \
    http://mirrors.mit.edu/centos/6.5/os/x86_64/

febootstrap-run ${tmpdir} -- sh -c 'echo "NETWORKING=yes" > /etc/sysconfig/network'

## set timezone of container to UTC
febootstrap-run ${tmpdir} -- ln -f /usr/share/zoneinfo/Etc/UTC /etc/localtime

febootstrap-run ${tmpdir} -- yum clean all

## xz gives the smallest size by far, compared to bzip2 and gzip, by like 50%!
febootstrap-run ${tmpdir} -- tar -cf - . | xz > centos65.tar.xz
EOF

chmod a+x build_centos.sh

cat << EOF > Dockerfile
From scratch

MAINTAINER Danil Ko

ADD centos6.5.tar.xz /

# Need to update additional selinux packages due to https://bugzilla.redhat.com/show_bug.cgi?id=1098120
RUN yum -y install yum install java-1.7.0-openjdk-devel wget http://mirror.centos.org/centos/6.5/centosplus/x86_64/Packages/libselinux-2.0.94-5.3.0.1.el6.centos.plus.x86_64.rpm http://mirror.centos.org/centos/6.5/centosplus/x86_64/Packages/libselinux-utils-2.0.94-5.3.0.1.el6.centos.plus.x86_64.rpm

# Use copy to preserve the the tar file without untar
COPY apache-tomcat-7.0.55.tar.gz /tmp/

RUN cd /opt; tar -xzf /tmp/apache-tomcat-7.0.55.tar.gz; mv /opt/apache-tomcat* /opt/apache-tomcat; rm /tmp/apache-tomcat-7.0.55.tar.gz;

ADD start-tomcat.sh /usr/local/bin/start-tomcat.sh

EXPOSE 8080

CMD ["/bin/sh", "-e", "/usr/local/bin/start-tomcat.sh"]

EOF

vagrant up

vagrant ssh "cd /vagrant; . build_centos.sh"

vagrant destroy

# Move the  centos_docker_builder folder to a docker host machine, in this example, it is the same machine
# Also download the tomcat file and name it as apache-tomcat-7.0.55.tar.gz

docker build -t centosimage .

# Run as a background daemon process and with the container name webtier
docker run -d --name webtier centosimage

# On a separate terminal, run (consider one only have the webtier running, otherwise, one will use docker ps -a to find the container id and use docker inspect )

docker inspect -f '{{ .NetworkSettings.IPAddress }}' webtier | grep IP
        "IPAddress": "172.17.0.60",
        "IPPrefixLen": 16,

# On a web browser, do

172.17.0.60:8080

The tomcat webpage will show up



To clean up the container, one may run

docker stop webtier; docker rm webtier;


To clean up the image, after the above command one may run

docker rmi centosimage;

1 comment: