Create optimized Docker image for Java/Spring Boot apps
✓Works with OpenClaudeYou are a Java/Spring Boot DevOps engineer. The user wants to create an optimized Docker image for their Java or Spring Boot application using best practices.
What to check first
- Verify your project has a
pom.xml(Maven) orbuild.gradle(Gradle) file in the root - Check that you have Docker installed:
docker --version - Confirm your Java application has a main class and can run:
mvn clean packageorgradle build
Steps
- Add the Google Jib Maven plugin to your
pom.xmlin the<plugins>section under<build>to enable containerization without writing a Dockerfile - Configure Jib with your target image registry (Docker Hub, GCR, or local Docker daemon) using
<configuration>tags - Set the base image to
eclipse-temurin:21-jreoropenjdk:21-slimfor a lightweight JVM runtime - Configure Jib to split layers: app dependencies, resources, and classes separately for better caching
- Add JVM flags like
-XX:+UseG1GCand-XX:MaxRAMPercentage=75.0in Jib's<jvmFlags>for production optimization - Build the image using
mvn compile jib:buildfor remote registry ormvn compile jib:dockerBuildfor local Docker - Test the image:
docker run --rm -p 8080:8080 your-image-name:latest - Verify logs and health endpoints respond correctly before pushing to production
Code
<!-- Add to pom.xml -->
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>spring-boot-app</artifactId>
<version>1.0.0</version>
<properties>
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<plugins>
<plugin>
<groupId>com.google.cloud.tools</groupId>
<artifactId>jib-maven-plugin</artifactId>
<version>3.4.0</version>
<configuration>
<from>
<image>eclipse-temurin:21-jre</image>
</from>
<to>
<image>docker.io/your-username/spring-boot-app</image>
<tags>latest,1.0.0</tags>
</to>
<container>
<jvmFlags>
<jvmFlag>-XX:+UseG1GC</jvmFlag>
<jv
Note: this example was truncated in the source. See the GitHub repo for the latest full version.
Common Pitfalls
- Treating this skill as a one-shot solution — most workflows need iteration and verification
- Skipping the verification steps — you don't know it worked until you measure
- Applying this skill without understanding the underlying problem — read the related docs first
When NOT to Use This Skill
- When a simpler manual approach would take less than 10 minutes
- On critical production systems without testing in staging first
- When you don't have permission or authorization to make these changes
How to Verify It Worked
- Run the verification steps documented above
- Compare the output against your expected baseline
- Check logs for any warnings or errors — silent failures are the worst kind
Production Considerations
- Test in staging before deploying to production
- Have a rollback plan — every change should be reversible
- Monitor the affected systems for at least 24 hours after the change
Related Java Skills
Other Claude Code skills in the same category — free to download.
Spring Boot Setup
Scaffold Spring Boot application with REST API
Java Testing
Set up JUnit 5 with Mockito and test containers
Maven/Gradle
Configure Maven or Gradle build system for Java projects
Spring Security
Configure Spring Security with JWT and OAuth2
Spring Data JPA
Set up Spring Data JPA with repositories and entities
Java Streams
Refactor loops to Java Streams and functional patterns
Java Virtual Threads (Project Loom)
Use Java 21+ virtual threads for high-concurrency I/O without the platform-thread overhead
Java Records and Pattern Matching
Use Java 21+ records and pattern matching for cleaner data classes
Want a Java skill personalized to YOUR project?
This is a generic skill that works for everyone. Our AI can generate one tailored to your exact tech stack, naming conventions, folder structure, and coding patterns — with 3x more detail.