This practical session demonstrates how to containerize a simple Java application using Docker. The experiment covers writing a Dockerfile, building a custom image, running a container, and verifying successful execution. The objective is to understand how Docker packages applications along with their runtime dependencies to ensure portability and consistency.
To create a Docker image for a Java application using a Dockerfile, build the image, and execute it successfully inside a container.
Class Practical 28 Jan/ │ ├── Container-Codes/ │ ├── Dockerfile │ └── Hello.java
public class Hello {
public static void main(String[] args) {
System.out.println("Hello, Docker!");
}
}
FROM ubuntu:22.04
RUN apt update && apt install -y openjdk-17-jdk
WORKDIR /home/app
COPY Hello.java .
RUN javac Hello.java
CMD ["java", "Hello"]
docker build -t java-app:1.0 .
Explanation:
-t assigns a tag to the imagejava-app is the image name1.0 is the version. indicates current directory as build contextdocker images
docker run java-app:1.0
Expected Output:
Hello, Docker!
This experiment illustrates how Docker encapsulates an application along with its runtime and dependencies into a portable image. By containerizing the Java program, we ensured environment consistency, simplified deployment, and improved application portability across systems.
Docker plays a crucial role in modern DevOps practices by enabling lightweight virtualization, scalable deployments, and reproducible environments.