DEV Community

Cover image for How to Dockerize a Java Application
Shorla
Shorla

Posted on • Edited on

How to Dockerize a Java Application

Docker makes it easy to build, manage, and deploy containerized applications. Using Docker during a development process helps to deliver a consistent environment across all machines. While working with a team, it is recommended to use Docker for your applications. This helps the dev cycle not to lag.

In this article, we are going to learn how to dockerize a Java application. However, the basic concepts apply to any technology you are using for your application.

Prerequisite

  • A conceptualized knowledge of Docker

  • An IDE

  • A Terminal — Linux or IDE Terminal

  • A source code

Steps to dockerizing a Java application

Creating a Java application

Let us create a simple “hello world” application in Java. We can do this by opening any IDE of our choice and creating a file named helloWorld.java. Then, we will copy and paste the code below into the file.

public class helloWorld{
public static void main(String[] args) {
System.out.println("Hello world!");
}
}

On the terminal, we will compile the code by running:

javac HelloWorld.java

Compiling the code creates a new file — “HelloWorld.class”. The “.class” file runs on the JVM to execute the application. To execute the application, we run:

java helloWorld

The terminal should print out the result Hello world!

Dockerizing the application

We will take the application we built and run it in a Docker container. To do that, it is assumed we have Docker installed on our computer. If not, we can do that here.

Once Docker is installed and started, the next step is to build a docker image for our application. We need a Dockerfile to build a docker image. To create the Dockerfile, we start by creating a new file and naming it Dockerfile. This file will include instructions on how to build the image for our application. The order of the command is important as each command will be executed consecutively.

We can copy and paste the following code to our Dockerfile. To learn more about keywords in Dockerfile, visit here.

# use the openjdk as the base image
FROM openjdk
# create a new app directory
RUN mkdir /app
# copy the app files from the host machine to the image filesystem
ADD java /app
# set the directory for executing future files
WORKDIR /app
# run the command for executing the application
CMD java HelloWorld

The “#” is used to write comments in Dockerfile. We can now build the image by running:

docker build -t hello-world:1.0 .

The “-t” option helps to tag the image with a name for easy access. In the code above, the name or tag assigned to the image is “hello-world:1.0”. The “.” at the end of the code is to specify that the Dockerfile is in the current directory.

if the error message below appears after running the code above, put sudo before running each code.

Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock:

Alternatively, we can add user to the Docker group. Instructions on how to do that can be found here.

The next step is to instantiate our image and run our container. This can be done by running:

docker run hello-world:1.0

The result 'Hello world!' should be printed on the terminal.

To see the list of images available, we can run:

docker image

To see the list of Docker containers currently running, we can run:

docker ps

Note: The container we just prompted will not be on the list. This is because it will be terminated after running.

Conclusion

Following the steps listed above, dockerizing a Java application should be clearer. It is advised to try the steps out on other applications to become more familiar with them.

I recommend reading this article for more information on docker.

Top comments (2)

Collapse
 
cicirello profile image
Vincent A. Cicirello

Or you can use Google's jib.

GitHub logo GoogleContainerTools / jib

🏗 Build container images for your Java applications.

stable Maven Central Gradle Plugin Portal Build Status Build Status Build Status SLSA 3 Gitter version

Jib

Jib - Containerize your Java applications.

☑️ Jib User Survey
What do you like best about Jib? What needs to be improved? Please tell us by taking a one-minute survey. Your responses will help us understand Jib usage and allow us to serve our customers (you!) better.

What is Jib?

Jib builds optimized Docker and OCI images for your Java applications without a Docker daemon - and without deep mastery of Docker best-practices. It is available as plugins for Maven and Gradle and as a Java library.

Maven: See documentation for jib-maven-plugin.
Gradle: See documentation for jib-gradle-plugin.
Jib Core: A general-purpose container-building library for Java.
Jib CLI: A command-line interface for building images that uses Jib Core.

For more information, check out the official blog post or watch this talk (slides).

Goals

  • Fast - Deploy your changes fast. Jib separates your application into multiple layers, splitting…

Collapse
 
shorla profile image
Shorla

Thank you for the contribution.