Reading Time: 4 minutes

Simple Guide To Dockerize Java Application Maven

How To Dockerize Java Application Maven with Dockerfile

Dockerize Java Application Maven: In this tutorial, we will dockerize a basic Java maven(quickstart) application.

Docker solves the problem of “it runs on my local machine”. Instead of delivering jars/wars, you deliver “images”.

Before beginning with this tutorial, please make sure you have Docker Engine installed in your system. Also, create an account on DockerHub.

Clone Project from Git Repo

Download the project files below from the GitHub URL.

docker project structure

What is a dockerfile?

Dockerfile is a plain text file with no extension present at the root directory of an application. This file contains a procedural script that needs to execute for the creation of the container that can build and run the application.

Dockerfile tells the Docker Engine the following information:

  1. What environment is required in a container for building the application?
  2. What environment is required in a container for running the application?
  3. What will the container look like and what all files it will have?

There is much other information that can be specified in dockerfile like port number, but for the scope of this tutorial, we will discuss only the necessary components required to run a simple Java application.

20 Java Regular Expressions Quiz Regex Questions [MCQ]

Create a Docker File

Create a file “dockerfile” in the root directory.

Below are some of the basic instructions that are used in dockerfile.

FROM: Tells the Docker to use the mentioned image as the base image. This image will be pulled from the docker hub.

AS: Is used to give an alias to imported images.

COPY: Copies files/directory from source to destination path.

RUN: Runs any UNIX command in the container’s terminal/command prompt.

CMD: Provides the facility to run UNIX commands to start the container. This can be overridden upon executing the docker run command.

ENV: Sets environment variables in the container’s path.

#: Is used to put a comment in the dockerfile.

This is the dockerfile that we will use for our application:

FROM maven:3.5.4-jdk-8-alpine as maven

COPY ./pom.xml ./pom.xml

COPY ./src ./src

RUN mvn dependency:go-offline -B

RUN mvn package

FROM openjdk:8u171-jre-alpine

WORKDIR /adevguide

COPY --from=maven target/SimpleJavaProject-*.jar ./SimpleJavaProject.jar

CMD ["java", "-jar", "./SimpleJavaProject.jar"]

Let’s discuss the significance of each line.

FROM maven:3.5.4-jdk-8-alpine as maven

Pull maven:3.5.4-jdk-8-alpine image from the docker hub and refer it as maven. This image has all the files required to build a maven application. Maven alpine is a minimized version of maven.

COPY ./pom.xml ./pom.xml

COPY ./src ./src

Copies the required files from the user system to Image. Docker will copy the src folder and pom.xml file into the image.

RUN mvn dependency:go-offline -B

Maven command to resolve application dependencies present in pom.xml.

RUN mvn package

Maven command to build an application using maven.

FROM openjdk:8u171-jre-alpine

Pull openjdk image from docker’s hub public repository. openjdk8 alpine is a minimized version of openjdk8.

WORKDIR /adevguide

Change the working directory to /adevguide

COPY --from=maven target/SimpleJavaProject-*.jar ./adevguide/SimpleJavaProject.jar

Copies build the jar from the previous maven image and put it in the working directory.

CMD ["java", "-jar", "./adevguide/SimpleJavaProject.jar"]

Execute the command in the CMD.

Docker Commands

We have learned how to create dockerfile in Dockerize Java Application Maven tutorial, let’s learn some basic docker commands.

Docker Build

docker build -t simplejavaproject .

docker build is used to build an application. -t specifies the image name. Image tag can be specified using the colon (:) ex simplejavaproject:2020. By default, the latest is used as a tag.

The dot(.) at end of the command specifies that build needs to happen in the current directory.

docker build 1

docker build 2

Docker Images

docker images

docker images display all images in the local docker engine.

docker images

Docker Run

docker run -i simplejavaproject

docker run

docker run is used to run an application. -i activate the interactive mode, which means STDIN remains attached to the terminal

Video Tutorials

The video explains an approach to Dockerize Java Application Maven.