Dockerizing a Java Application: A Step-by-Step Guide

Dockerizing your Java application can provide numerous benefits, including consistent deployment environments, ease of scaling, and simplified dependency management. This guide will walk you through the process of creating a Docker image for your Java application.


  • JDK 17 or later
  • Maven or Gradle
  • Docker installed on your machine
  • IDE (IntelliJ IDEA, Eclipse, etc.)

Step 1: Set Up a Java Application

For this tutorial, we'll create a simple Java application using Maven. If you already have a Java application, you can skip this step.

1.1 Create a New Maven Project

You can use your IDE to create a new Maven project or use the command line.

mvn archetype:generate -DgroupId=com.example -DartifactId=my-java-app -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
cd my-java-app

1.2 Create a Simple Java Application

Edit the file in the src/main/java/com/example directory.

package com.example;

public class App {
    public static void main(String[] args) {
        System.out.println("Hello, Docker!");

1.3 Update pom.xml

Ensure that your pom.xml is configured correctly. The default configuration provided by the Maven archetype should suffice.

<project xmlns=""


  • maven-compiler-plugin: Configures the Java compiler.
  • maven-assembly-plugin: Packages the application into a jar with dependencies.

Step 2: Build the Java Application

Run the following command to build the jar file of your Java application:

mvn clean package

This command will generate a jar file in the target directory, for example, target/my-java-app-1.0-SNAPSHOT-jar-with-dependencies.jar.

Step 3: Create a Dockerfile

Create a file named Dockerfile in the root directory of your project. This file will contain instructions for building the Docker image.

# Use the official OpenJDK base image
FROM openjdk:17-jdk-alpine

# Set the working directory inside the container

# Copy the built jar file into the container
COPY target/my-java-app-1.0-SNAPSHOT-jar-with-dependencies.jar app.jar

# Expose port 8080 (if your application runs on a specific port)

# Run the application
CMD ["java", "-jar", "app.jar"]


  • FROM openjdk:17-jdk-alpine: Uses the official OpenJDK base image with JDK 17.
  • WORKDIR /app: Sets the working directory inside the container to /app.
  • COPY target/my-java-app-1.0-SNAPSHOT-jar-with-dependencies.jar app.jar: Copies the built jar file to the container.
  • EXPOSE 8080: Exposes port 8080 (modify if your application uses a different port).
  • CMD ["java", "-jar", "app.jar"]: Runs the application.

Step 4: Build the Docker Image

Run the following command to build the Docker image:

docker build -t my-java-app .


  • docker build: The Docker command to build an image.
  • -t my-java-app: Tags the image with the name my-java-app.
  • .: Specifies the current directory as the build context.

Step 5: Run the Docker Container

Run the following command to start a Docker container from the image:

docker run -p 8080:8080 my-java-app


  • docker run: The Docker command to run a container.
  • -p 8080:8080: Maps port 8080 of the container to port 8080 on the host machine.
  • my-java-app: The name of the Docker image to run.

Step 6: Verify the Application

Open a web browser or a tool like Postman and navigate to the following URL (if your application has a web interface):


For this tutorial, since the application prints "Hello, Docker!" to the console, you can check the console output to see the message.

Additional Docker Commands

6.1 List Docker Images

To list all Docker images on your system, run:

docker images

6.2 List Running Containers

To list all running Docker containers, run:

docker ps

6.3 Stop a Running Container

To stop a running Docker container, run:

docker stop <container_id>

Replace <container_id> with the actual container ID obtained from the docker ps command.

6.4 Remove a Docker Container

To remove a Docker container, run:

docker rm <container_id>

Replace <container_id> with the actual container ID.

6.5 Remove a Docker Image

To remove a Docker image, run:

docker rmi my-java-app


In this guide, you have learned how to dockerize a Java application. We covered:

  • Setting up a simple Java application.
  • Writing a Dockerfile to build and run the Java application.
  • Building the Docker image.
  • Running the Docker container.
  • Verifying the application.

By following these steps, you can easily package your Java application into a Docker container, providing a consistent and portable deployment environment.