| name | docker-springboot-best-practices |
| description | Use this skill when building, optimizing, or deploying Docker images for Spring Boot applications. |
When building Docker images for Spring Boot:
-
Use multi-stage builds:
- build stage → compile application (Maven/Gradle)
- runtime stage → run application with minimal JRE
- do not include build tools in final image
-
Base images:
- build stage → maven:3.9-eclipse-temurin-17 or gradle image
- runtime stage → eclipse-temurin:17-jre-alpine or similar minimal image
-
Build process:
- copy only required files (pom.xml/build.gradle first)
- leverage Docker layer caching
- run build with tests skipped only if handled in CI
-
Artifact handling:
- copy generated JAR into runtime image
- do not include source code in final image
-
Security:
- run container as non-root user
- minimize installed packages
- avoid exposing unnecessary ports
- never include secrets in image
-
Configuration:
- use environment variables for configuration
- externalize application properties
- avoid hardcoding credentials or URLs
-
Ports:
- expose application port explicitly (e.g. 8080)
-
JVM optimization:
- configure memory settings (e.g. -Xms, -Xmx)
- use container-aware JVM options
-
Health checks:
- define HEALTHCHECK using actuator endpoints
- ensure readiness and liveness probes are available
-
Logging:
- log to stdout/stderr
- do not write logs to local files inside container
-
Image tagging:
- use semantic versioning (MAJOR.MINOR.PATCH)
- optionally include commit hash or build metadata
-
Reproducibility:
- use fixed base image versions
- ensure deterministic builds
- avoid latest tags in production
-
Avoid:
- single-stage Dockerfiles
- running as root
- embedding secrets in image
- large images with unnecessary dependencies
- tight coupling between image and environment
-
Refactor by:
- introducing multi-stage builds
- reducing image size
- externalizing configuration
- improving caching strategy
-
Ensure:
- minimal and secure runtime image
- fast build and startup times
- consistent behavior across environments
- alignment with container best practices
If the image includes unnecessary dependencies, runs as root, or is not reproducible, it violates this skill.