How to Deploy Java Apps With Docker

Source – dzone.com

For those new to Docker, let me say “Welcome to the party!” It’s an easy way to deploy, run, and manage applications using VM-like containers that are independent of elements like hardware and language, which makes these containers highly portable. And it’s all the rage.

So how do you deploy Java apps using Docker? You’re in the right place. I’ll walk through the process step by step – from installing Docker to installing Java inside a Docker container to deploying and running an app.

Setting Up for the Tutorial

Docker supports just about any OS you’d care to use:

Linux

Mac

Windows

Windows Server

AWS

Azure

I’ll be using Linux for this demonstration. For the app, I’ll use Bitbucket Server (Atlassian’s behind-your-firewall Git repository manager), but you can substitute any other Java application you like.

Ok. Now we can create a commit to save the state of this container in an image.

Exit the container (with exit or CTRL-d) and list the containers – dead or alive with docker ps -a, which outputs:

ID IMAGE COMMAND CREATED STATUS PORTS

8e07a84ea97a base:latest /bin/bash 12 minutes ago Exit 0

fecada4ce303 base:latest /bin/bash 17 minutes ago Exit 0

9cb541022c5b base:latest /bin/bash 25 minutes ago Exit 127

a2914a38394d durdn/base:latest /bin/bash 26 minutes ago Exit 0

6fa304872025 durdn/base:latest /bin/bash 30 minutes ago Exit 0

3e0241227129 durdn/base:latest /bin/bash 30 minutes ago Exit 0

98b400fcb5dc durdn/base:latest /bin/bash 31 minutes ago Exit 0

88a113234c47 base:latest /bin/bash 36 minutes ago Exit 0

From the list of recent containers we take the most recent, where we installed all the dependencies. Now we can commit a new snapshot/image with docker commit:docker commit 8e07a84ea97a durdn/java7

And as you can see, it is now listed if I type docker images:

REPOSITORY TAG ID CREATED

durdn/java7 latest ab6396541f9a 2 hours ago

base ubuntu-quantal b750fe79269d 10 weeks ago

base ubuntu-quantl b750fe79269d 10 weeks ago

base latest b750fe79269d 10 weeks ago

base ubuntu-12.10 b750fe79269d 10 weeks ago

Step 3: Install the App in Your Docker Container

Now we can install our Java application – in this case Bitbucket Server – in our newly created durdn/java7 image.

We can add content to an image in several ways: the docker insert command is one, but we can also use curl, wget, etc.

Spin up a shell in the newly created durdn/java7 image opening a mirror port 7990 from the container to the host and with a persistent home where the data will be stored:docker run -i -t -p :7990 -v /opt/stash-home durdn/java7 /bin/bash
root@298af82e71ef:/#

Download Bitbucket Server. (Note that when this post was originally published, Bitbucket Server was called “Stash”. You’ll see that here in some of the file and directory names, as well as the screenshot belowroot@298af82e71ef:/# curl -Lks https://www.atlassian.com/software/stash/downloads/binary/atlassian-bitbucket-4.13.0.tar.gz -o /root/stash.tar.gz

To avoid startup errors we must add the unique id of this host 298af82e71ef to the /etc/hosts directory.

Now we can start the app:root@298af82e71ef:/# /opt/stash/bin/start-stash.sh -fg

Check that Stash Bitbucket Server is running in the container by accessing http://localhost:7990/stash. It works:

We can now exit this container and commit it so that we can reuse the work we did:docker commit aec2feb8cdea durdn/stash
effd5d47b34f

Where aec2feb8cdea was the ID of the last modified container in docker ps -a. The result is that we have a new image called durdn/stash with Bitbucket Server installed:

REPOSITORY TAG ID CREATED

durdn/java7 latest ab6396541f9a 2 hours ago

durdn/stash latest effd5d47b34f 3 seconds ago

base ubuntu-quantal b750fe79269d 10 weeks ago

base ubuntu-quantl b750fe79269d 10 weeks ago

base latest b750fe79269d 10 weeks ago

base ubuntu-12.10 b750fe79269d 10 weeks ago

A Note About Ephemeral Containers and Persistent Storage

Containers are ephemeral. Once torn down, they will be reset entirely to their snapshot state!

This means your app installation will be reset every time you stop the container. To make sure data is preserved between runs we can use volumes, which will be shared and persisted between containers. We do this simply by reusing the volumes from older containers using the option -volumes-from.

Let’s start Bitbucket in the container interactively (-i runs the command interactively and -t attaches a tty to it):

Voilá! Your application instance – with data persisted – is now running in the background as you can see by running docker ps.

More Docker Goodies to Explore

What I like most about Docker is how responsive and quick it is, and the instant repeatability it offers. Spinning up a new container takes literally the same time it takes to run the command on bare metal. It’s a joy to behold! Since this post was originally published in 2013, my fellow Atlassians and I have written several more articles about Docker. What can I say?… we’re in love. You might find some of them useful:

Docker all the things: automation and wiring

Building web apps with Docker and Bamboo

Docker containers, Bamboo, and winning at continuous delivery

Common Dockerfile mistakes

Docker clusters from the ground up: front-end reverse proxy

And for the truly container-addicted, there’s Bitbucket Pipelines – the easiest way to build, test, and deploy from your Bitbucket repo. Builds run in isolated Docker containers on infrastructures that Atlassian manages, so there’s no need to set up and configure your own build agents. I highly recommend it.