How to deploy a Java application as a Docker Container
Srinivasan (Srini) Viswanathan
Service Delivery Leader | DevOps Engineer| Kubernetes | Terraform | CI/CD | Containers| AWS | GCP | Observability | GKE | EKS
In this blog post, we will demonstrate the process of utilizing a HelloWorld.java sample application, constructing a JAR file via Maven, and subsequently executing the application within a Docker container.
Tools thats are used for this application are, VSCode for writing Java application , GITHUB for source code repository, Maven for building and Docker.
You can download source code from GIT HUB as mentioned below.
git clone https://github.com/srinivish/my-java-app.git
The folder structure employed for developing the Java application is as follows: C:\my-java-app\src\main\java\com\example\HelloWorld.Java. Referencing the code snippet below:
# cd my-java-app/src/main/java/com/example/HelloWorld.java
package com.example;
public class MyApp {
public static void main(String[] args) {
System.out.println("Hello, world!");
}
}
Upon obtaining the source code, navigate to the project root using the change directory command and execute the Maven package command to generate a JAR file. This process involves downloading various dependencies and thus may require some time to complete. The resulting JAR file is located in the target directory.
mvn clean install -DskipTests
With the JAR file ready, the subsequent step involves crafting a Docker image containing the JAR file and the requisite runtime environment. To Dockerize the application, a Dockerfile is utilized, containing instructions for building the Docker image encapsulating the Java application and its dependencies.
Create a file named Dockerfile in the root directory of your Maven project, and populate it with the following content:
领英推荐
# Use a base image with Java installed
FROM openjdk:8-jdk-alpine
# Set the working directory inside the container
WORKDIR /app
# Copy the compiled Java application JAR file into the container
COPY target/my-java-app-1.0-SNAPSHOT.jar /app/my-java-app.jar
# Command to run the Java application
CMD ["java", "-jar", "my-java-app.jar"]
Building the Docker Image:
To construct the Docker image for the Java application, execute the subsequent command within the root directory of the Maven project.
docker build -t my-java-app .
This command constructs a Docker image named my-java-app by leveraging the directives outlined in the Dockerfile.
Running the Docker Container:
Once the Docker image is successfully built, initiate a Docker container based on that image using the following command:
docker run --name java-container -d my-java-app
This command initializes a Docker container based on the my-java-app image, granting access to the running Java application. Utilizing the system.out method, one can inspect the Docker logs to verify the proper execution of the JAR file and the corresponding message written in the log.
Conclusion:
Deploying a sample Java application in Docker via Maven build entails crafting a Dockerfile to bundle the application into a Docker image, followed by launching a Docker container rooted in that image. Docker furnishes a consistent environment for application execution.