2. Apache Hello World

Cloud by Example
David Winterfeldt
Version 0.10
Copyright © 2015 David Winterfeldt
Table of Contents
Preface ..................................................................................................................................... iii
1. Applications in the Cloud ................................................................................................. iii
2. Goals of This Book ......................................................................................................... iv
3. A Note about Format ....................................................................................................... iv
I. Docker Introduction .................................................................................................................. 5
A Practical Introduction to Docker ......................................................................................... 6
1. Apache Server ......................................................................................................... 6
2. Apache Hello World ................................................................................................. 7
3. Apache SSH ........................................................................................................... 8
II. Docker In-depth .................................................................................................................... 10
Contact Application ........................................................................................................... 11
1. Contact - Single Tier .............................................................................................. 11
A. Setup .................................................................................................................................. 12
A.1. Docker Ubuntu Setup .................................................................................................. 12
Ubuntu Setup ........................................................................................................... 12
Update & Install Software ........................................................................................... 12
Docker Installation .................................................................................................... 12
Expose Docker API Port ............................................................................................. 13
B. Admin ................................................................................................................................. 14
B.1. Docker Commands ..................................................................................................... 14
B.2. Docker Bind Port ........................................................................................................ 15
B.3. Keep Docker Process from Exiting ................................................................................. 15
C. Terminology Mapping ............................................................................................................ 16
D. Author Bios ......................................................................................................................... 17
D.1. David Winterfeldt ....................................................................................................... 17
Introduction ............................................................................................................. 17
Technical Expertise ................................................................................................... 17
Experience ............................................................................................................... 19
Sites & Blogs ........................................................................................................... 19
Contact Info ............................................................................................................. 19
Cloud by Example Version 0.10
Cloud by Example
ii
Preface
David Winterfeldt
2015
The cloud, everyone is talking about it. But how do you, as a developer or someone in operations, use it? Once
something is deployed to the cloud, it still needs to be managed an updated.
One of the most interesting developments in the virtualization and cloud space is Docker
[https://www.docker.com/]. It provides an engine that can run Linux processes, which are similar to running a VM
but much lighter. For a good, basic explanation on the differences between Docker [https://www.docker.com/] and
VMs read What is Docker? [https://www.docker.com/whatisdocker/ ].
While some other cloud examples may be shown, this book will primarily focus on how Docker
[https://www.docker.com/] can help.
1 Applications in the Cloud
There are multiple ways to deploy your applications to the cloud. A general term for this area is Cloud Computing
[http://en.wikipedia.org/wiki/Infrastructure_as_a_service]. The main attributes associated with it are agility, cost,
virtualization, multi-tenancy, scalability, elasticity, and performance. Some of the main categories for Cloud
Computing [http://en.wikipedia.org/wiki/Infrastructure_as_a_service] are PaaS (Platform as a Service), SaaS
(Software as a Service), and IaaS (Infrastructure as a Service).
Google App Engine and VMware Cloud Foundry are well known PaaS solutions. They provide a lot if you can use
them. Because they typically provide a lot features & scalability for any applications done on them. But they also
create specific restrictions to provide these features. For example, on Google App Engine, you can't choose any type
of persistent store as you typically might when creating an application that will run on your own hardware.
Saas typically provides services on demand for a subscription fee. For that price you get the service available to you
without having to worry about licensing the software, installation, infrastructure, backup, etc. An example of this is
Google Documents enterprise version.
IaaS provides on demand infrastructure, which is basically VMs (Virtual Machines). A Virtual Machine is a
virtualized instance of a server. Virtualization is being widely adopted and is the foundation for the Cloud. One key
driving factor is that it provides a great deal of convenience and cost savings to organizations. Whether the
infrastructure is hosted or in house. Rackspace & Amazon EC2, Bluelock provide hosted services with on demand
VMs. vCloud & Xen provide internal and external solutions for providing VMs on demand.
Docker [https://www.docker.com/] basically provides an Image and a Container for creating and managing
processes. An Image is a representation of the Linux OS, Services, and anything else installed on it that has been
committed using Docker [https://www.docker.com/] version control. This Image can then be pushed or pulled from
repositories. From the Image, a Container can be created, which is the running Linux process.
Docker [https://www.docker.com/] provides a foundation that makes it fairly easy to create elastic clouds,
application & service modelling, collaboration & management tools, etc. Many companies including Docker
[https://www.docker.com/] are working on providing more functionality every day. It's an area that is rapidly
changing and continues to grow very quickly.
Cloud by Example Version 0.10
Cloud by Example
iii
Preface
2 Goals of This Book
This book's goal is to provide hands on examples for taking applications and deploying them to the cloud. From
basic to complicated applications and Services. Also, to provide some Tips & Tricks for using Docker
[https://www.docker.com/] as well as best practices and making the most of out it's features.
3 A Note about Format
New terms appear in bold italics the first time they are defined or described. Buzzwords, or keywords that have
already been defined but that are important to a concept being explained may then appear in italics. Direct
references to coding examples in the text of the book (methods, classnames, etc), and the coding examples
themselves, are displayed in courier font, otherwise known as That Typewriter Typeface (TTT).
Incidentally, there are a lot of acronyms in this book and some of them may be new to you. Generally if the acronym
is the most common way to refer to a particular term, we will use the term, followed by the acronym, the first time it
is mentioned in a section. The acronym will be used for the remainder of the section.
Cloud by Example Version 0.10
Cloud by Example
iv
Part I. Docker Introduction
This site will provide a basic introduction to Docker [https://www.docker.com/]. Docker [https://www.docker.com/]
provides a way to easily create, share, and distribute virtual images and deploy them in a resource efficient way
under Linux.
A Practical Introduction to Docker
David Winterfeldt
2015
Basic examples creating images and running them.
Note
Most examples will require a directory to be created, and then the content shown added to specified
file. Above the content should be the directory and file name (ex: my/Dockerfile).
1. Apache Server
Create an Apache Server image. Static files can be places in the /var/www/html directory.
cbe-apache/Dockerfile
FROM centos:centos6
MAINTAINER Cloud by Example
RUN yum update -y; yum clean all
RUN yum -y install httpd; yum clean all
RUN mkdir -p /var/www/html
EXPOSE 80
USER root
ENTRYPOINT /usr/sbin/httpd -DFOREGROUND
The command builds the image against the cbe-apache directory, and tags it with cbe:apache.
$ sudo docker build --rm --tag=cbe:apache cbe-apache
Show the list of images available with the command below. If it's a clean installation, there should be the newly
created cbe:apache image and the centos:centos6 image it depended on.
$ sudo docker images
REPOSITORY
TAG
cbe
apache
centos
centos6
IMAGE ID
456e98607a04
510cf09a7986
CREATED
7 seconds ago
3 weeks ago
VIRTUAL SIZE
374.4 MB
202.6 MB
This command runs the cbe:apache image, and binds it's exposed port 80 (which Apache is running on) to the host's
Cloud by Example Version 0.10
Cloud by Example
6
A Practical Introduction to Docker
port 80 so Apache is accessible.
$ sudo docker run -d -p 80:80 cbe:apache
Show the list of containers running with the command below. There should be the newly created container based on
the cbe:apache image showing the internal port 80 is bound to the host's port 80.
$ sudo docker ps
CONTAINER ID
PORTS
b30ec99095b6
0.0.0.0:80->80/tcp
IMAGE
NAMES
cbe:apache
goofy_colden
COMMAND
CREATED
STATUS
"/bin/sh -c '/usr/sb
3 seconds ago
Up 2 seconds
Go to the IP address of the docker server and the default Apache home page should load.
2. Apache Hello World
Using the cbe:apache image.
Note
The first line uses the cloudbyexample/cbe:apache image, which pulls from the Cloud by Example
Repo [https://hub.docker.com/u/cloudbyexample/] on Docker Hub [https://hub.docker.com/]. Instead
of the cbe:apache tag from the previous example.
apache-hello-world/Dockerfile
FROM cloudbyexample/cbe:apache
MAINTAINER Cloud by Example
ADD ./index.html .
RUN mv index.html /var/www/html;chmod 644 /var/www/html/index.html
apache-hello-world/index.html
<html>
<head>
<title>Cloud by Example </title>
</head>
<body>
<h1>Hello World</h1>
</body>
</html>
Cloud by Example Version 0.10
Cloud by Example
7
A Practical Introduction to Docker
The first command builds the image. The second runs it and binds any exposed port to a random, available host port.
$ sudo docker build --rm --tag=cbe:apache-hello-world apache-hello-world
$ sudo docker run -d -P cbe:apache-hello-world
Show the list of running containers running, and there should be the running cbe:apache-hello-world image. The
port column shows '0.0.0.0:49153->80/tcp', which indicates the host port 49153 has been bound to the container's
port 80.
$ sudo docker ps
CONTAINER ID
IMAGE
PORTS
NAMES
7f31ba0f55aa
cbe:apache-hello-world
0.0.0.0:49153->80/tcp
sharp_carson
COMMAND
CREATED
STATUS
"/bin/sh -c '/usr/sb
4 seconds ago
Up 4 seconds
3. Apache SSH
Using the cbe:apache image, SSH access will be added.
apache-ssh/Dockerfile
FROM cloudbyexample/cbe:apache
MAINTAINER Cloud By Example
RUN yum -y install openssh-server; yum clean all
RUN echo "root:password" > passwd_file;chpasswd < passwd_file;rm passwd_file
EXPOSE 22
ENTRYPOINT service sshd start && /usr/sbin/httpd -DFOREGROUND
Run the following commands.
$ sudo docker build --rm --tag=cbe:apache-ssh apache-ssh
$ sudo docker run -d -P cbe:apache-ssh
Looking at the running container, port 80 is exposed on 49155 and the SSH port (22) is exposed on port 49154.
$ sudo docker ps
CONTAINER ID
IMAGE
COMMAND
PORTS
c37b5c09294d
cbe:apache-ssh
"/bin/sh -c 'service
0.0.0.0:49154->22/tcp, 0.0.0.0:49155->80/tcp
Cloud by Example Version 0.10
Cloud by Example
CREATED
STATUS
4 seconds ago
Up 3 seconds
8
A Practical Introduction to Docker
SSH into the server using the command below by substituting the correct IP Address and port.
$ ssh -p 49154 [email protected]
Note
Since this is based on the default cloudbyexample/cbe:apache, there is just the default homepage. To
change this, create an index.html and add anything (or copy the Hello World index.html example in
Apache Hello World) to see it show up as the new home page for the container.
$ vi /var/www/html/index.html
Cloud by Example Version 0.10
Cloud by Example
9
Part II. Docker In-depth
In-depth Docker [https://www.docker.com/] examples of application will be covered.
Contact Application
David Winterfeldt
2015
1. Contact - Single Tier
contact-single-tier/Dockerfile
FROM cloudbyexample/cbe:tomcat-7.0.57
MAINTAINER Cloud by Example
# add webapp
RUN cd /usr/local/tomcat/webapps;wget
http://cloudbyexample.org/repo/cbe/0.10/contact-single-tier-webapp/contact.war;chmod 755
/usr/local/tomcat/webapps/contact.war;
Run the following commands.
$ sudo docker build --rm --tag=cbe:contact-single-tier contact-single-tier
$ sudo docker run -d -P cbe:contact-single-tier
The contact application should now be accessible at 'http://${host.ip}:${host.port}/contact'.
Cloud by Example Version 0.10
Cloud by Example
11
Appendix A. Setup
Setup your favorite Linux OS that Docker [https://www.docker.com/] supports. Below is a quick setup guide for
Ubuntu Server [http://www.ubuntu.com/server].
Note
While developing, there were issues with boot2docker on a Mac. Using Ubuntu Server
[http://www.ubuntu.com/server] on VMware Fusion worked well without any issues.
A.1. Docker Ubuntu Setup
Ubuntu Setup
For a quick setup, use Ubuntu Server either on a standalone machine or using the download image to create a VM
(Virtual Machine).
1. Download and setup Ubuntu Server (Ubuntu Server Download [http://www.ubuntu.com/download/server]).
Update & Install Software
Update Ubuntu Server [http://www.ubuntu.com/server] and install software.
Note
Installing SSH is optional, but you'll like want to be able to SSH into your Docker
[https://www.docker.com/] host.
sudo apt-get -y update
sudo apt-get -y install openssh-server curl
Docker Installation
Get the latest version of Docker [https://www.docker.com/].
curl -s http://get.docker.io/ubuntu/ | sudo bash -
Cloud by Example Version 0.10
Cloud by Example
12
Setup
Expose Docker API Port
Edit the Docker [https://www.docker.com/] config and change the IP Address and port to the correct values for you
host.
Note
This step is optional, but will allow direct communication to the Docker [https://www.docker.com/]
API on the IP Address and port specified.
sudo vi /etc/default/docker
DOCKER_OPTS="-H tcp://192.10.24.128:12983 -H unix:///var/run/docker.sock"
Restart Docker [https://www.docker.com/] for the changes to take effect.
sudo service docker restart
An easy way to test this is to get the info for the server. (ex: http://192.10.24.128:12983/info)
Cloud by Example Version 0.10
Cloud by Example
13
Appendix B. Admin
This isn't meant to be a comprehensive guide, but basic user & admin tips and commands will be covered. Please
refer to Docker [https://www.docker.com/] documentation for a more details.
B.1. Docker Commands
Table B.1. Docker Commands
Name
Description
Command
List Processes
List running processes (containers).
$ sudo docker ps
List Recent Processes
Lists recently run processes.
$ sudo docker ps -l
List Processes with Full ID
Show Container Name
Commit a Container
Run a Container with Shell
Run a Container Service with Shell
Lists with
truncation).
full
identifier
(no
$ sudo docker ps --no-trunc
Show container name based on it's
identifier.
$ sudo docker inspect -f "{{
.Name }}" ${container.id}
Commit a running container based
on it's identifier.
$ sudo docker commit
${container.id} cbe:tomcat
Run a container in interactive mode
with the bash shell.
$ sudo docker run -i -t
ubuntu /bin/bash
Override the default entry point.
Useful for debugging images if there
are any issues.
$ sudo docker run -i -t
--entrypoint /bin/bash -P
cbe:tomcat
Note
Use with caution since these commands will remove all images or containers.
Table B.2. Docker Admin Commands
Name
Description
Remove All Containers
Remove all containers running or
exited.
Cloud by Example Version 0.10
Cloud by Example
Command
$ sudo docker ps -a -q |
xargs sudo docker stop |
xargs sudo docker rm -f
14
Admin
Name
Description
Remove All Images
Remove
all
images,
intermediaries from builds.
Command
even
$ sudo docker images -q |
xargs sudo docker rmi -f
B.2. Docker Bind Port
To bind the Docker [https://www.docker.com/] API to an external IP Address & port, add the following to the
'docker.conf' file.
/etc/init/docker.conf
DOCKER_OPTS="-H tcp://${host.ip}:${host.port} -H unix:///var/run/docker.sock"
B.3. Keep Docker Process from Exiting
Since Docker [https://www.docker.com/] runs processes, if the process exits that stops everything. Even if there
were background services running. A way to get around this is to add, && tail -f /dev/null just after the entry point
command.
/etc/init/docker.conf
ENTRYPOINT /usr/sbin/service tomcat start && tail -f /dev/null
Cloud by Example Version 0.10
Cloud by Example
15
Appendix C. Terminology Mapping
Basic mappings of Docker [https://www.docker.com/] terms to standard Virtual Machine terminology.
Table C.1. Terminology Mappings
Docker
Virtual Machine
Image
VM Template
Container
VM Instance
Cloud by Example Version 0.10
Cloud by Example
16
Appendix D. Author Bios
D.1. David Winterfeldt
Introduction
David has been doing software development for over 20 years. He's been using Java since 1998 and involved in
using Open Source almost as long. David has focused on Web and Enterprise development for most of his career,
and started working with the Spring Framework in 2006. He started working with Spring 2.0 towards the end of
2006 and really enjoy working with the Spring Framework. He really enjoys it because it not only saves time, but
encourages better design and code reuse through loosely coupled components.
David started Spring by Example [http://www.springbyexample.org/] to post different examples he had been doing,
and also to become involved again with Open Source projects. Spring by Example
[http://www.springbyexample.org/] is a general resource for Spring and should ultimately save developers time.
Currently
David
works
at
VMware
on
the
VMware
vFabric
Application
Director
[http://www.vmware.com/products/application-platform/vfabric-application-director/overview.html] project. It
enables developers and organizations to deploy applications to the cloud by having a logical abstraction for software
services and application topologies. This allows an application to be easily deployed multiple times to different
environments.
David is also a committer on Struts [http://struts.apache.org/] and Commons Validator
[http://commons.apache.org/validator/], but is no longer active on either. He was also the creator of Commons
Validator [http://commons.apache.org/validator/].
Technical Expertise
• Publications
• Author of Cloud by Example [http://cloudbyexample.org/], which is a free online book for the Cloud, primarily
focused on Docker [https://www.docker.com/].
• Co-author of Spring In-depth, In Context [http://www.springindepth.com/], which is a free, but incomplete
(stopped due to time constraints), online book for the Spring Framework.
• Co-author of the validation chapter in Struts in Action: Building Web Applications with the Leading Java
Framework, Manning Publications (November 2002) [http://www.manning.com/husted/].
• Conference Speaker
• Speaker at VMware Partner Exchange 2013 [http://www.partnerexchangeontour2013.com/] doing a demo of
VMware
vFabric
Application
Director
[http://www.vmware.com/products/application-platform/vfabric-application-director/overview.html] in a break
out session.
• Speaker at NY Java SIG [http://www.javasig.com/] in January 2013 presenting Deploying Spring Apps to
Amazon EC2 and VMware vCloud [http://javasig-spring.eventbrite.com/].
Cloud by Example Version 0.10
Cloud by Example
17
Author Bios
• Speaker at Skills Matter [http://skillsmatter.com/] in London, December 2012 presenting Automated
Provisioning
of
Spring
Apps
to
EC2
&
vCloud
[http://skillsmatter.com/event/java-jee/automated-provisioning-of-spring-apps-to-ec2-vmware-vcloud].
• Speaker at SpringOne 2GX 2012 [http://www.springone2gx.com/] presenting Automated Provisioning of
Spring
Apps
to
EC2
&
VMware
vCloud
[http://www.springone2gx.com/topics/automated_provisioning_of_spring_apps_to_ec2__vmware_vcloud].
• Speaker at SpringOne 2GX 2010 [http://www.springone2gx.com/] presenting Killer Flex RIAs with Spring
ActionScript [http://www.springone2gx.com/conference/chicago/2010/10/session?id=19344].
• Speaker at SpringOne Americas 2008 [http://www.springone2gx.com/] presenting Case Study: GWT & Comet
Integration
with
the
Spring
Framework
at
NYSE
[http://www.springone2gx.com/m/mobile/presentation.jsp?showId=172&presentationId=12842].
• Open Source Contributions
• Original developer of Commons Validator and its integration into Struts.
• Committer
on
Struts
(http://struts.apache.org)
(http://jakarta.apache.org/commons).
and
the
Commons
Validator
• Webmaster of Spring by Example (http://www.springbyexample.org [http://jakarta.apache.org/commons]).
• Architecture & Design
• Struts Validator & Commons Validator with matching client side JavaScript validation
• Scalable E-mail Framework with scheduling, retry process, template processing, and dynamic population
processing
• Keyword based E-mail Alert System for online articles
• Scalable Publishing System with configurable publishing processes
• Multi-threaded, multi-tier servers with custom thread pools and tcp/ip socket connections for a client entry
prototype system.
• Programming Languages
• Java 1.0/1.1.x/1.2.x/1.3.x/1.4.x/1.5/1.6/1.7, Perl, JavaScript, SQL, PL/SQL
• Java EE
• JDBC, RMI, JavaMail, JMS, EJBs (Stateless, Session, Message Driven, Entity), RMI, JSPs, Servlets, Web
Services, XML, JTA
• Application Servers
• Jetty 6.x, Tomcat 3.x/4.x/5.0/5.5/6.0/7.0.x, Spring dm Server, JBoss, BEA Weblogic Application Server
4.0/4.5/5.1/7.0
• Databases
Cloud by Example Version 0.10
Cloud by Example
18
Author Bios
• Oracle 8i/9i, DB2, MS SQL*Server 6.0/6.5/7.0, MySQL, PostgreSQL, MS Access, FoxPro
• Open Source Frameworks
• Spring Framework, Spring Security, Spring Data JPA, Spring Web Flow, Struts, GWT, Hibernate, Velocity
Template Engine, Axis, OSGi
• Open Source Projects
• Ant, Maven, AspectJ, Dozer, Jakarta Commons BeanUtils, Jakarta Commons Betwixt, Jakarta Commons
Collections, Jakarta Commons Digester, Jakarta Commons IO, Jakarta Commons Lang, Jakarta Commons
Validator, Jakarta ORO, Jakarta POI, Jakarta Regexp, Tiles, Log4J, LOGBack, Xalan, Xerces, JUnit, HttpUnit,
Cactus, XDoclet
• Miscellaneous
• Windows 2000 Server, Windows XP, Mac OS X, Solaris, Linux, Eclipse IDE
Experience
• Lead Architect: Designing systems/frameworks, preparing projects and frameworks for developers on new
projects, providing instruction and assistance to developers.
• Ability to interact with Business Analysts, and Clients to gather requirements and implement.
• Performance Enhancements: database performance, adding multi-threading, optimizing code.
• Interaction with clients to define specifications, gather requirements, and communicate & resolve issues.
• Documentation: Javadocs, Wiki based documentation for developers, technical specifications.
• Interpreting specifications and requirement documents into working systems.
• Ability to learn new technologies, investigate issues, resolve development issues.
• Working with distributed team environments.
Sites & Blogs
• Spring by Example - http://springbyexample.org/
• Cloud by Example - http://cloudbyexample.org/
• Blog - http://davidwinterfeldt.blogspot.com/
• Spring by Example Blog - http://springbyexample.blogspot.com/
• Cloud by Example Blog - http://cloudbyexample.blogspot.com/
• Twitter - http://twitter.com/dwinterfeldt
Cloud by Example Version 0.10
Cloud by Example
19
Author Bios
Contact Info
E-mail: <[email protected]>
Cloud by Example Version 0.10
Cloud by Example
20