This project demonstrates how to dockerize a Java application by creating a Dockerfile, building a Docker image, and running it as a container in Windows PowerShell. This repository provides a complete guide and necessary files to containerize a simple Java application.
- A step-by-step guide to containerizing a Java application.
- Usage of Docker to ensure consistent runtime environments.
- Build, run, and test the containerized application on any system with Docker installed.
-
Docker Installed on Windows: Install Docker Desktop for Windows and ensure it is running. Download Docker Desktop
-
Java Development Kit (JDK):
Ensure JDK is installed to compile the Java application. -
Windows PowerShell:
Open a terminal to execute Docker commands.
├── Dockerfile # Instructions for building the Docker image
├── src/ # Java source code directory
│ └── Main.java # Simple Java application entry point
├── README.md # Project documentation
└── target/ # Compiled Java application (created after build)
└── app.jar # Packaged JAR file of the application
Clone this GitHub repository to your local machine:
git clone https://github.com/your-username/java-docker-project.git
cd java-docker-project
Navigate to the src/
folder and compile the Main.java
file:
javac -d ../target src/Main.java
Then package the compiled code into a .jar
file:
jar -cvf target/app.jar -C target .
Ensure the Dockerfile
in the root directory contains the following:
# Use OpenJDK 17 base image
FROM openjdk:17-jdk-alpine
# Set the working directory in the container
WORKDIR /app
# Copy the JAR file into the container
COPY target/app.jar app.jar
# Run the application
CMD ["java", "-jar", "app.jar"]
Use the following command in PowerShell to build the Docker image:
docker build -t java-app .
Start a container using the built image:
docker run -it java-app
After running the container, you should see the output of your Java application in the terminal.
docker images
docker ps # List running containers
docker stop <CONTAINER_ID>
docker rm <CONTAINER_ID>
- Add support for environment variables.
- Automate builds with GitHub Actions.
- Include a multi-stage build for more efficient image sizes.
This project is licensed under the MIT License. See the LICENSE
file for more details.
Feel free to contribute to this project by opening issues or submitting pull requests! 😊