Docker for Java Developers- Deploying Java applications in Docker containers

Many java developers are familiar with deploying java applications via jar files and application containers such as Tomcat however many organizations are now deploying applications with Docker containers. Lets take a look at the bare minimum steps to get a java application running in a Docker container.

Install Docker

In order to run your java app in a container your are going to need Docker. Once installed Docker will allow you to build and run your containers on your local machine. It runs on many platforms including Linux, MacOS, and Windows. Head over to the Docker website and look for information on the supported platforms.

Once installed you can test out your installation by building and running the hello-world container. The output below indicates your installation was a success.

▲ :zsiegel (master)$ docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
d1725b59e92d: Pull complete
Digest: sha256:0add3ace90ecb4adbf7777e9aacf18357296e799f81cabc9fde470971e499788
Status: Downloaded newer image for hello-world:latest

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:

For more examples and ideas, visit:

Create a Dockerfile for your Java app

Now that you have Docker installed lets take a simple Java app built by Gradle and create a Dockerfile. We are going to both build and run the jar file within the container itself which gives us a more consistent environment. You can checkout my sample project on Github and work from this project if you would like.

Now that we have our basic Java app lets take a look at what a Dockerfile might look like.

# NOTE: This is not a production ready Dockerfile.
# Utilize this only for development purposes

# Use a container image that has both Gradle and the JDK
FROM gradle:5.0.0-jdk8-alpine

# Switch to the gradle user defined by our container image
USER gradle

# Copy over the project directory into the container
COPY --chown=gradle:gradle . /java-and-docker

# Set our working directory to our project directory that we set above
WORKDIR /java-and-docker

# Run the build
RUN gradle build

# Run the jar file
# Since we are using JDK8 we set some additional flags to be more container aware
CMD ["java", "-XX:+UnlockExperimentalVMOptions", "-XX:+UseCGroupMemoryLimitForHeap", "-jar", "build/libs/java-and-docker-1.0.jar"]

Build and Run your Java app in the Docker container

Now that we have our Dockerfile created we can ask docker to first build and then run our container with the following commands.

docker build -t zsiegel:java-and-docker .
docker run zsiegel:java-and-docker


Now that you have successfully built and run a java app within a Docker container you can start exploring the related articles. These will help you understand how the java runtime and your apps will behave depending on the JVM version and the container flags you set. This is important because running java in a container is not as straightforward as it may seem.