Hi!
In this tutorial we’re going to set up a simple Java application to build, deploy,
and run inside a Docker container!
We’re going to set up the start to a continuous integration environment using Java, Maven
and Docker.
If you’re not familiar with Maven, there’s a Maven playlist of three videos to get you
up to speed.
Docker is getting very ubiquitous, but if you’re not familiar with Docker let me know
in the comments and I’ll create more Docker tutorials.
Here's our goal for this tutorial.
We will compile the Java code, and on a successful build, we’ll package it up into a jar, and
create a Docker container containing our Java application.
We’ll do all of this within Maven.
The Maven script can be called from any continuous integration tool.
From there we can push the docker file to a Docker repository, or we can use it to spin
up as many instances of the machine that we want.
That’s the power of Docker.
We define our container once, and we can run it as many times as we want.
This is important for microservices.
Let’s start by creating a Hello World application using Maven.
Enter this into a command line on your computer.
All the source and commands from this tutorial are listed on DeegeU.com so make sure you
check that out.
At this point we have a simple Hello World application.
If we compile and run it, it simply prints “Hello World”.
This is the auto-generated Java application from the Maven quickstart archetype.
And this is what our geenrated POM file looks like.
This project is a bare bones Java project, but we should be able to apply the next steps
to any Java project.
The next thing we want to do is add a plugins section to our POM file.
This goes right under the dependencies section.
We’re going to add a plugin which helps us create Dockerfiles.
There are several plugins to do this, but the one we will use in this tutorial is the
one from Spotify.
The most recent version of the Spotify Maven plugin is 0.4.10 [as of the video date], so
we’ll use that one.
We want our Java application added to the Dockerfile after we’re finished compiling
and testing.
This happens in the package phase when we perform the build goal.
Every docker container starts with the user name and then the container name, separated
with a slash like this.
To make the code easier to change, I’m using the project name as the container name.
We could also make it very generic and make a property for the user name if we want.
For a company it might make sense to parameterize the user name.
We can also specify a private repository if we need to.
To do this, we’d also need to specify a server and url.
The server credentials should be added to your Maven settings.xml file.
The XML added looks similar to this, but you'd need to provide your own credentials.
You'll find the settings.xml file in the .m2 directory on your machine.
We’ll start our Docker image with the Java 8 base image.
This is an official Java docker image.
Well… as official as it gets.
This is the OpenJDK version of Java running on Debian Linux.
You can create a Docker image using Oracle’s JDK, but with licensing issues it gets tricky
for someone to distribute one.
In a Dockerfile, we would create a line using the ENTRYPOINT command.
We could also use the CMD command, but ENTRYPOINT makes sense when you’re creating a container
where the application you specify should be the only thing runnable.
That’s what we want in this case.
The reason for this is we want our Docker container to be immutable and created for
a single purpose.
That means there should be only one way to start our Docker instance.
Finally we want to specify where our jar should be installed in the Docker image.
We'll specify the target directory in the resources section of the plugin.
We'll put our jar in the /opt directory.
We also need to specify what to copy.
So we need to specify the jar file, and the project directory where to find it.
To build our Docker image, we can run the Maven command mvn clean package docker:build.
This will build our application, and create a Dockerfile.
When we want to push the image to a Docker repository, we'll add the flag -DpushImage
to our command.
We'd do this in a CI build, but we probably would not include the flag for developer builds.
So this is something you'd specify in your build tool, like Jenkins.
Once we've successfully built our Dockerfile, we can run it like any other container.
If we had more complex requirements for our Dockerfile, we can include a Dockerfile in
our POM file.
We would specify a directory containing our Dockerfile using the dockerDirectory element
in the plugin.
If we do this, baseImage, maintainer, cmd, and entryPoint elements are ignored.
That's all there is to adding Docker to our Maven builds.
Once we've pushed the image to a repository, we can use a script to pull the image and
launch as many instances as we need.
If you have any questions, please let me know in the comments below or on DeegeU.com.
If you want to be notified when new videos are out, you can subscribe to the DeegeU.com
channel on YouTube, or you can subscribe to the DeegeU.com newsletter.
The newsletter goes out once a month, and we won't give your email address to anyone.
I'll see you in the next video!