Skip to content

Softsysteme/javaee-docker-java

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

77 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Docker and Kubernetes for Java Developers

Preface

Containers are enabling developers to package their applications (and underlying dependencies) in new ways that are portable and work consistently everywhere? On your machine, in production, in your data center, and in the cloud. And Docker has become the de facto standard for those portable containers in the cloud.

Docker is the developer-friendly Linux container technology that enables creation of your stack: OS, JVM, app server, app, and all your custom configuration. So with all it offers, how comfortable are you and your team taking Docker from development to production? Are you hearing developers say, “But it works on my machine!” when code breaks in production?

This lab offers developers an intro-level, hands-on session with Docker, from installation, to exploring Docker Hub, to crafting their own images, to adding Java apps and running custom containers. It will also explain how to use Swarm to orchesorchestrate these containers together. This is a BYOL (bring your own laptop) session, so bring your Windows, OSX, or Linux laptop and be ready to dig into a tool that promises to be at the forefront of our industry for some time to come.

Prerequisites

This lab can be executed on a netbook with x86 architecture. For it to be fun, you should have a decent hardware available. Beside the operating system of choice, there’s only a couple of other thing you should have installed already:

Hardware

Mac

  1. CPU

    1. PowerPC G4

    2. x64 (i5 and comparable)

  2. Memory

    1. 4 to 8 GB for attendees

    2. 8 to 16 GB for instructors

Linux / Windows

  1. CPU

    1. x86 (Intel Pentium)

    2. x64 (i5 and comparable)

  2. Memory

    1. 4 to 8 GB for attendees

    2. 8 to 16 GB for instructors

Software

Mac

  1. Operating System

    1. Mac OS X (10.6 or later)

  2. Java

  3. Webbrowser

Linux / Windows

  1. Operating System

    1. Windows 7 (SP1)

    2. Fedora 21

  2. Java

  3. Webbrowser

Setup Environments

This section describes the relevant steps for both attendees and instructors to setup the environments. Please follow the parts, that are appropriate for you.

Instructor

The instructor setup is designed to make the lab most reliable even with bad internet connections. Most if not all of the software can be directly downloaded from the instructor machine. The machine is setup as docker host and also runs a docker registry. Please make sure to execute these instructions way prior to the lab to provide a good experience to the attendees.

Attendees

This lab is designed for a BYOL (Brying Your Own Laptop) style hands-on-lab. We did our best to support a wide range of client configurations but only did test on machines as stated in the hardware section. Please make sure to double check your configuration.

The Lab

Docker Basics

Docker simplifies software delivery by making it easy to build and share images that contain your application’s entire environment, or application operating system.

What does it mean by application operating system ?

Your application typically require a specific version of operating system, application server, JDK, database server, may require to tune the configuration files, and similarly multiple other dependencies. The application may need binding to specific ports and certain amount of memory. The components and configuration together required to run your application is what is referred to as application operating system.

You can certainly provide an installation script that will download and install these components. Docker simplifies this process by allowing to create an image that contains your application and infrastructure together, managed as one component. These images are then used to create Docker containers which run on the container virtualization platform, provided by Docker.

What can a Java Developer use Docker for?

Faster delivery of your applications

Docker helps you with the development lifecycle. Docker allows you to develop on local containers that contain your applications and services. It can then integrate into a continuous integration and deployment workflow.

For example, you write code locally and share the development stack via Docker with colleagues. When everybody is ready, you push the code and the stack you all are developing onto a test environment and execute any required tests. From the testing environment, you can then push the Docker images into production and deploy your code.

Deploying and scaling more easily

Docker’s container-based platform allows for portable workloads. Docker containers can run on a developer’s local host, on physical or virtual machines in a data center, or in the Cloud.

Docker’s portability and lightweight nature also make dynamically managing workloads easy. You can use Docker to quickly scale up or tear down applications and services. Docker is so fast, that scaling can be near real time.

How is it different from VM?

Docker is an open source container virtualization platform.

Docker has three main components:

  1. Images are build component of Docker and a read-only template of application operating system.

  2. Containers are run component of Docker, and created from, images.Containers can be run, started, stopped, moved, and deleted.

  3. Images are stored, shared, and managed in a registry, the distribution component of Docker. The publically available registry is known as Docker Hub.

In order for these three components to work together, there is Docker Daemon that runs on a host machine and does the heavy lifting of building, running, and distributing Docker containers. In addition, there is Client that is a Docker binary which accepts commands from the user and communicates back and forth with the daemon.

docker architecture
Figure 1. Docker architecture

Client communicates with Daemon, either co-located on the same host, or on a different host. It requests the Daemon to pull an image from the repository using pull command. The Daemon then downloads the image from Docker Hub, or whatever registry is configured. Multiple images can be downloaded from the registry and installed on Daemon host. Images are run using run command to create containers on demand.

How does a Docker Image work?

We’ve already seen that Docker images are read-only templates from which Docker containers are launched. Each image consists of a series of layers. Docker makes use of union file systems to combine these layers into a single image. Union file systems allow files and directories of separate file systems, known as branches, to be transparently overlaid, forming a single coherent file system.

One of the reasons Docker is so lightweight is because of these layers. When you change a Docker image—for example, update an application to a new version— a new layer gets built. Thus, rather than replacing the whole image or entirely rebuilding, as you may do with a virtual machine, only that layer is added or updated. Now you don’t need to distribute a whole new image, just the update, making distributing Docker images faster and simpler.

Every image starts from a base image, for example ubuntu, a base Ubuntu image, or fedora, a base Fedora image. You can also use images of your own as the basis for a new image, for example if you have a base Apache image you could use this as the base of all your web application images.

Note: Docker usually gets these base images from Docker Hub.

Docker images are then built from these base images using a simple, descriptive set of steps we call instructions. Each instruction creates a new layer in our image. Instructions include actions like:

  1. Run a command.

  2. Add a file or directory.

  3. Create an environment variable.

  4. What process to run when launching a container from this image.

These instructions are stored in a file called a Dockerfile. Docker reads this Dockerfile when you request a build of an image, executes the instructions, and returns a final image.

How does a container work?

A container consists of an operating system, user-added files, and meta-data. As we’ve seen, each container is built from an image. That image tells Docker what the container holds, what process to run when the container is launched, and a variety of other configuration data. The Docker image is read-only. When Docker runs a container from an image, it adds a read-write layer on top of the image (using a union file system as we saw earlier) in which your application can then run.

Docker Machine

Machine makes it really easy to create Docker hosts on your computer, on cloud providers and inside your own data center. It creates servers, installs Docker on them, then configures the Docker client to talk to them.

Once your Docker host has been created, it then has a number of commands for managing them:

  1. Starting, stopping, restarting

  2. Upgrading Docker

  3. Configuring the Docker client to talk to your host

You used Docker Machine already during the attendee setup. We won’t need it too much further on. But if you need to create hosts, it’s a very handy tool to know about. From now on we’re mostly going to use the docker client. Find out more about the details at the Official Docker Machine Website

Check if docker machine is working with

docker-machine -v

Docker Client

The client communicates with the demon process on your host and let’s you work with images and containers. Check if your client is working with

docker -v

The most important options you’ll be using frequently are:

  1. run - runs a container

  2. ps- lists containers

  3. stop - stops a container

Get a full list of available commands with

docker

Ticket Monster

TicketMonster is an example application that focuses on Java EE6 - JPA 2, CDI, EJB 3.1 and JAX-RS along with HTML5 and jQuery Mobile. It is a moderately complex application that demonstrates how to build modern web applications optimized for mobile & desktop. TicketMonster is representative of an online ticketing broker - providing access to events (e.g. concerts, shows, etc) with an online booking application.

Apart from being a demo, TicketMonster provides an already existing application structure that you can use as a starting point for your app. You could try out your use cases, test your own ideas, or, contribute improvements back to the community.

ticket monster tutorial architecture
Figure 2. TicketMonster architecture

The application uses Java EE 6 services to provide business logic and persistence, utilizing technologies such as CDI, EJB 3.1 and JAX-RS, JPA 2. These services back the user-facing booking process, which is implemented using HTML5 and JavaScript, with support for mobile devices through jQuery Mobile.

The administration site is centered around CRUD use cases, so instead of writing everything manually, the business layer and UI are generated by Forge, using EJB 3.1, CDI and JAX-RS. For a better user experience, Twitter Bootstrap is used.

Monitoring sales requires staying in touch with the latest changes on the server side, so this part of the application will be developed in HTML5 and JavaScript using a polling solution.

Build Ticket Monster

First thing, you’re going to do is to build the application from source. Create a folder for the source and change to it

mkdir /docker-java/
cd /docker-java/

And checkout the sources from the instructor git repository.

git clone -b WFLY8.1 http://root:dockeradmin@<INSTRUCTOR_IP>:10080/root/ticket-monster.git

From here, you’re free to explore the application a bit. Open it with JBDS and find more background about the use-cases and how the application is designed at the Ticket Monster Website.

Copy the Maven lab-settings.xml file that you have downloaded from the instructor machine and place it inside /docker-java

When you’re ready, it is time to build the application. Switch to the checkout directory and run maven package.

cd /docker-java/ticket-monster
mvn -s lab-settings.xml -f demo/pom.xml package

Congratulations: You just build the applications war file. Let’s see if this can be deployed.

Run Ticket Monster For The First Time

The application needs two things from an infrastructure perspective. A WildFly application server and a Postgress Database. Let Docker do the magic for us.

Check if your docker host is running

docker-machine ls

If the machine state is stopped, starte it with

docker-machine start

After it is started you can find out about the IP address of your host with

docker-machine ip

We’ll use this from now on as <HOST_IP> in the commands.

Now we need a WildFly and a database. Start with the Postgres database.

docker run --name db -d -p 5432:5432 -e POSTGRES_USER=ticketmonster -e POSTGRES_PASSWORD=ticketmonster-docker <INSTRUCTOR_IP>:5000/postgres

This command starts a container named "db" from the image in your instructor’s registry "<INSTRUCTOR_IP>:5000/postgres". As this will not be present locally, it needs to be downloaded first. But you’ll have a very quick connection to the instructor registry and this shouldn’t take long. The two "-e" options define environment variables which are read by the db at startup and allow us to access the database with this user and password. Finally, the "-d" option tells docker to start a demon process. "-p" maps container ports to host ports and allows other containers on our host to access it.

This should have worked. To double check if it did, you can see the server logs

docker logs -f db

The "-f" flag keeps refreshing the logs and pushes new events directly out to the console.

After the database server is up and running we now need the WildFly.

docker run -d --name wildfly -p 8080:8080 --link db:db -v /Users/youruser/tmp/deployments:/opt/jboss/wildfly/standalone/deployments/:rw <INSTRUCTOR_IP>:5000/wildfly

This command starts a container named "wildfly" and links this container to the db (--link option) container we started earlier.

Note
More Information about container linking

You saw how you can connect to a service running inside a Docker container via a network port. But a port connection is only one way you can interact with services and applications running inside Docker containers. Docker also has a linking system that allows you to link multiple containers together and send connection information from one to another. When containers are linked, information about a source container can be sent to a recipient container. This allows the recipient to see selected data describing aspects of the source container. See more about container communication on the Docker website Linking Containers Together

The "-v" flag maps a local directory into the host. This will be the place to put the deployments. Please make sure to use -v /c/Users/ notation for drive letters on windows. The other options are known to you already. Check the logs if the server is started.

docker logs -f wildfly

And access the http://<HOST_IP>:8080 with your webbrowser to make sure the instance is up and running.

Now you’re ready to deploy the application for the first time. Let’s use JBoss Developer Studio for this.

Deploy an application from JBoss Developer Studio

Start JDBS if not started. And create a server adaptor first.

jbds1
Figure 3. Server adapter

Assign or create a WildFly 8.x runtime (Changed properties are highlighted.)

jbds2
Figure 4. WildFly Runtime Properties

Setup the server properties in the following image. The two properties on the left are automatically propagated from the previous dialog. Additional two properties on the right side are required to disable to keep deployment scanners in sync with the server.

jbds3
Figure 5. Server properties

Specify a custom deployment folder on Deployment tab of Server Editor

jbds4
Figure 6. Server Editor

Right-click on the newly created server adapter and click “Start”.

jbds5
Figure 7. Start Server

Now you need to right-click, Run on Server on the ticket-monster application and chose this server. The project runs and displays the start page of ticket-monster

jbds6
Figure 8. Start Server

Congratulations! You’ve just deployed your first application to a WildFly running in a Docker container.

Stop wildfly

docker stop wildfly

Other Deployment options

Let’s explore deeper, what other deployment options we have

Deployment to WildFly Container using Management API

Using the CLI

Using the web console

Ticket-Monster Docker Cluster

Cluster using Swarm

We don’t setup Kubernetes on Windows yet (might be an option to use jube someday)

Docker Compose (Optional, No Windows Version without Python)

References

  1. JBoss and Docker: http://www.jboss.org/docker/

Releases

No releases published

Packages

 
 
 

Contributors

Languages

  • HTML 99.9%
  • Other 0.1%