Docker Java Program: One-Stop Solution for Developers


In this article, we will learn about dockerize the simple Java Program with easy to understand steps.

What is Docker?

Docker is an open-source platform designed to automate the deployment, scaling, and management of applications within software containers. Containers are lightweight and portable virtualized environments that package an application and all its dependencies (libraries, runtime, system tools, etc.) needed to run it. Docker provides a way to create, distribute, and run these containers, making it easier for developers to build, ship, and run applications consistently across different environments.

Some of the key components of Docker includes -

  • Docker Engine
  • Docker Image
  • Docker Container
  • Dockerfile

Dockerize the Java Program

To run a Java program inside a Docker container, you’ll need to create a Dockerfile to define the container image and then build and run the container. Below are the steps to achieve this -

Step 1: Create the Java program

First, create a simple Java program. Let’s assume you have a file named with the following content:

public class Main {
    public static void main(String[] args) {
        System.out.println("Hello World!");
Step 2: Create a Dockerfile

Next, create a Dockerfile in the root directory where the Java file is present.

A Dockerfile is a plain text configuration file used to define the instructions for building a Docker image. It provides a set of commands and parameters that describe how the base image should be modified, what software and dependencies should be installed, and how the container should be configured when it runs.

Create a file named Dockerfile with the following content:

# Use a Java base image
FROM openjdk:8

# Set the working directory inside the container

# Copy the Java program to the container
COPY . /app/

# Compile the Java program
RUN javac

# Set the entry point to run the compiled Java program
ENTRYPOINT ["java", "Main"]

Let’s discuss some of the important Dockerfile commands that you use in your day to day development -

  • FROM Command: It is used to specify the base image from which you want to build your own custom Docker image. It is the name of the base image you want to use. It can be an official image from Docker Hub or a custom image that you or someone else has created and pushed to a container registry.

  • WORKDIR Command: It is used to set the working directory inside the container where any subsequent instructions will be executed. It helps to define a relative path for your commands, making the Dockerfile more readable and maintainable. You can specify an absolute path or a relative path in the container. If the directory does not exist, Docker will create it for you.

  • COPY Command: It is used to copy files and directories from the host machine (the system where Docker is running) into the container being built. It allows you to include application code, configuration files, and other necessary resources into the image.

  • RUN Command: It is used to execute commands during the image build process. You can use the RUN command to install packages, run setup scripts, create directories, and perform various other tasks required to set up the environment inside the container.

  • ENTRYPOINT Command: It is used to specify the default executable for the Docker container. It sets the primary command that will be executed when you run a container from the built image. The ENTRYPOINT instruction is different from the CMD instruction in that it does not provide default arguments for the command; instead, it defines the actual executable.

Step 3: Build the Docker image

Open a terminal or command prompt, navigate to the directory containing your Java program and the Dockerfile, and run the following command to build the Docker image

docker build -t helloworld-java-program .

The -t flag allows you to tag the image with a name, and helloworld-java-program is the name we’re giving to the image.

Step 4: Run the Docker container

Now that the image is built, you can run the Docker container based on that image

docker run my-java-app

This will start the container, and you should see the output -

Hello World!

That’s it! You’ve successfully run a Java program inside a Docker container.

Watch the video

You can watch the below video to understand more about dockerizing the Java program.


Docker simplifies the development workflow by allowing developers to work in consistent environments, regardless of the underlying infrastructure. It also helps DevOps teams by streamlining the deployment process, enabling easy scaling, and promoting a microservices-based architecture.

Because of its efficiency and portability, Docker has gained widespread popularity in the software industry, making it easier for developers to build, test, and deploy applications with confidence.