Java Example in Docker
To run a Java application in Docker, you can containerize it by creating a Dockerfile that defines the environment for running your Java app. I'll walk you through a simple example of how to do this.
Steps to Create a Dockerized Java Application:
We'll use a basic Java Spring Boot application in this example, but you can adapt these steps for any Java application.
1. Prepare a Java Application
First, create a simple Java application. For this example, let’s assume you have a Spring Boot application that runs on port 8080.
Sample Spring Boot Application (Application.java):
If you're using Spring Boot, the main class might look like this:
package com.example.demo;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublic class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); }}
pom.xml for Maven (if you're using Maven):Here’s a basic pom.xml that defines the Spring Boot app’s dependencies.
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId> <artifactId>demo</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build></project>
Run the application using Maven:
mvn spring-boot:run
2. Create a Dockerfile
Next, create a Dockerfile to containerize your application.
Example Dockerfile:
# Step 1: Use an official OpenJDK base imageFROM openjdk:17-jdk-slim# Step 2: Set the working directory in the containerWORKDIR /app# Step 3: Copy the JAR file to the working directoryCOPY target/demo-0.0.1-SNAPSHOT.jar app.jar# Step 4: Expose port 8080 for the Spring Boot appEXPOSE 8080# Step 5: Define the command to run your appENTRYPOINT ["java", "-jar", "app.jar"]
Here’s what each part of the Dockerfile does:
- FROM openjdk:17-jdk-slim: This line sets the base image to an official OpenJDK image that includes the JDK.
- WORKDIR /app: This creates a working directory in the container where all your application files will be copied.
- COPY target/demo-0.0.1-SNAPSHOT.jar app.jar: Copies the JAR file (built from your Spring Boot app) into the container.
- EXPOSE 8080: Exposes port 8080 so that the app can be accessed externally.
- ENTRYPOINT: Specifies the command to run the Spring Boot JAR file when the container starts.
3. Build the Application JAR
Before building the Docker image, you need to build your Spring Boot application JAR file.
Run the following Maven command to build the JAR file:
mvn clean package
This will create a JAR file (e.g., demo-0.0.1-SNAPSHOT.jar) in the target/ directory of your project.
4. Build the Docker Image
Once you have the Dockerfile and the JAR file, you can build the Docker image using the docker build command.
In your project root (where the Dockerfile is located), run:
docker build -t java-spring-boot-app .
This command will build a Docker image with the tag java-spring-boot-app.
5. Run the Docker Container
Once the image is built, you can run your Java application in a Docker container using the following command:
docker run -p 8080:8080 java-spring-boot-app
-p 8080:8080: This maps port8080on the container to port8080on your host machine, which is the port used by Spring Boot by default.
This will run your Java Spring Boot application in a Docker container, and you can access it by visiting http://localhost:8080 in your browser.
6. Test Your Java Application
Once your container is running, you can test it by navigating to the exposed port (http://localhost:8080). If your Spring Boot app has a simple /hello endpoint, you could try:
curl http://localhost:8080/hello
If everything is working properly, you should see a response from your Java app.
7. Optional: Push Your Docker Image to Docker Hub
If you want to share your Docker image, you can push it to Docker Hub. First, log in to Docker Hub:
docker login
Then, tag your image:
docker tag java-spring-boot-app yourdockerhubusername/java-spring-boot-app
Finally, push it:
docker push yourdockerhubusername/java-spring-boot-app
Conclusion
With these steps, you’ve successfully containerized your Java application (in this case, a Spring Boot app) using Docker. Now, you can run your Java application anywhere Docker is available, ensuring that it runs consistently across different environments.
If you're using a different Java framework or just a simple Java application, you can adapt the Dockerfile and these steps accordingly.