Dockerize Hello World Java Program

Hey guys in this article, we will discuss how to dockerize the simple Hello World Java Program with easy to understand steps.

Read More:

Watch the Video Tutorial

Download and Install Docker

First, we need to download docker desktop and install it on our system. Docker Desktop is available for all platforms. Go ahead, download and install docker desktop on your system.

Once you have installed it, you can verify whether docker is installed correctly or not by executing the following command,

$ docker -v

This would display the docker version if you installed it correctly.

$ Docker version 20.10.14, build a224086

Next, we need to start the docker daemon on your system.

$ sudo systemctl start docker

Alternatively, you can start docker by clicking on the docker icon. This will take a few seconds to start the docker daemon.  Once it is started, it will show the message Docker desktop is running.

Development Steps

Below are the development steps to dockerize the Hello World Java program.

  • Create a Java file
  • Write Hello World Java program
  • Create a docker file
  • Build the docker image
  • Run the docker container

Create a Java file

Create a folder on your desktop with a name dockertutorial

$ mkdir dockertutorial

Inside the folder, create a file with a name and open the file in any text editor.


Write Hello World Java Program

Inside the file, write the below code

public class Main {
    public static void main(String[] args) {
        System.out.println("Hello World");

Create a Dockerfile

In order to dockerize, we need to create a Dockerfile. Inside the dockertutorial folder, create a file with name Dockerfile. This file does not have any extension and the first letter should be in capital letter.

FROM openjdk:8


COPY . /app/

RUN javac

ENTRYPOINT [ "java", "Main" ]

This dockerfile contains the series of instructions that how to run the java program. We will tell docker to follow these instructions to dockerize it.

In this article you will learn about creating JPQL query for containing condition with easy to understand example

  • FROM: Docker file starts with FROM instruction, we will tell docker to use one of the base image to run the Java program. In order to run the java code, we need Jdk. The base image name is openjdk:8, docker uses this as a base image to compile and run the java code. FROM instruction is similar to import statement in java. 
  • WORKDIR: We will use this instruction to set the base directory for the docker image.
  • COPY: This command takes two parameters, source and destination. We will use command to copy files from source to destination.
  • RUN: We will use this instruction to run any command. In order to compile the java program we will use RUN command.
  • ENTRYPOINT: We will use this command to run the program/application. In order to run the compiled java file, we will use ENTRYPOINT instruction. This takes array, we will pass the instruction in comma seperated.

Build the Docker image

Once the Dockerfile is ready, we will can docker image using the Dockerfile. We will use Docker build command create the docker image.


$ Docker build <option> <repository-name>:<tag-name> buildPath

We will use the below command to build the docker image in the current directory

Docker build -t bushan1992/docker-helloworld .


  • -t is the tag name
  • bushan1992/docker-helloworld is the repository name
  • . indicates the current directory

Once the image is build, we can see the list of images that are present.

$ docker images

We can see the list of images that are present


Run the docker container

Next, we need to start the container to run the image

$ docker run bushan1992/docker-helloworld

This will display the message to the console


That’s it for this post, if you like this post, share this with your friends and colleagues or you can share this within your social media platform. Thanks, I will see you in our next post.

Bushan Sirgur

Hey guys, I am Bushan Sirgur from Banglore, India. Currently, I am working as an Associate project in an IT company.

This Post Has One Comment

  1. sudhakar reddy

    I am Sudhakar, I need one help in docker.How to run java play application shut down hooks in docker image.

Leave a Reply