You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Update Docker examples to use JDK_JAVA_OPTIONS best practice
- Replaced JAVA_OPTS with JDK_JAVA_OPTIONS in all Docker examples
- JDK_JAVA_OPTIONS is automatically applied to all Java processes
- Provides better consistency and flexibility in containerized environments
- Updated both Dockerfile and Kubernetes deployment examples
- Added explanation in goal section about JDK_JAVA_OPTIONS benefits
- Added safeguard recommendation to prefer JDK_JAVA_OPTIONS over JAVA_OPTS
- Simplified ENTRYPOINT commands since JDK_JAVA_OPTIONS is automatic
- Updated generated markdown file in .cursor/rules/ directory
Copy file name to clipboardExpand all lines: .cursor/rules/130-java-docker-best-practices.md
+47-39Lines changed: 47 additions & 39 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -12,25 +12,42 @@ You are a Senior DevOps Engineer and Java Developer with extensive experience in
12
12
13
13
Provide comprehensive guidance for containerizing Java 25 applications using Docker best practices. Focus on creating efficient, secure, and maintainable Docker containers that leverage Java 25's latest features including Virtual Threads, improved container awareness, and modern JVM optimizations.
14
14
15
-
### Key Areas Covered:
16
-
-**Multi-stage builds** for optimal image size and security
17
-
-**Java 25 specific optimizations** including Virtual Threads and container-aware JVM settings
18
-
-**Security hardening** with non-root users and minimal attack surface
19
-
-**Performance tuning** for containerized Java applications
20
-
-**Development workflow** optimization for faster iteration
21
-
-**Production deployment** best practices
22
-
-**Monitoring and observability** integration
15
+
### Implementing These Principles
23
16
17
+
These guidelines are built upon the following core principles:
18
+
19
+
1.**Multi-Stage Build Optimization**: Separate build and runtime environments to create lean, secure final images. Use appropriate base images (JDK for build, JRE for runtime) and minimize the number of layers while maximizing Docker layer caching efficiency.
20
+
21
+
2.**Java 25 Container Awareness**: Leverage Java 25's enhanced container support with proper JVM configuration for containerized environments. Enable container-aware memory management, configure appropriate garbage collectors, and utilize Virtual Threads for I/O-intensive applications.
22
+
23
+
3.**Security-First Approach**: Implement defense-in-depth security practices including non-root users, read-only root filesystems, minimal attack surface, regular security scanning, and proper secrets management. Never embed sensitive information in Docker images.
24
+
25
+
4.**Performance and Resource Optimization**: Configure JVM settings for container resource limits, implement proper health checks, optimize image layers for faster builds and deployments, and use appropriate resource constraints in orchestration platforms.
26
+
27
+
5.**Development Workflow Enhancement**: Create development-friendly configurations with hot-reload capabilities, remote debugging support, and efficient local development environments using Docker Compose with proper volume mounts and networking.
28
+
29
+
6.**Production Readiness**: Implement comprehensive monitoring, logging, and observability features. Configure proper graceful shutdown handling, implement circuit breakers and timeouts, and ensure scalability with container orchestration platforms.
30
+
31
+
7.**Modern Java Features Integration**: Take advantage of Java 25's Virtual Threads, Pattern Matching, Records, and improved garbage collection algorithms. Configure JVM flags specifically for containerized environments using `JDK_JAVA_OPTIONS` environment variable for automatic application to all Java processes.
32
+
33
+
8.**Container Orchestration Best Practices**: Design containers for Kubernetes and other orchestration platforms with proper resource limits, health checks, service discovery, and configuration management through ConfigMaps and Secrets.
34
+
35
+
9.**Build and Deployment Pipeline**: Integrate Docker builds into CI/CD pipelines with proper image tagging strategies, security scanning, and multi-environment deployment configurations.
36
+
37
+
10.**Monitoring and Observability**: Implement comprehensive logging to stdout/stderr, expose metrics endpoints, configure distributed tracing, and integrate with monitoring solutions like Prometheus, Grafana, and APM tools.
24
38
25
39
## Constraints
26
40
27
-
Before applying Docker containerization, ensure the Java application builds successfully and follows Java 25 best practices.
41
+
Before applying Docker containerization recommendations, ensure the Java application is in a valid state and follows Java 25 best practices. Application compilation failure is a BLOCKING condition that prevents any containerization work.
28
42
29
-
-**MANDATORY**: Verify the application builds with `./mvnw clean package` before containerization
30
-
-**VERIFY**: Ensure Java 25 is properly configured in the project's pom.xml
31
-
-**SECURITY**: Always use non-root users in production Docker images
32
-
-**PERFORMANCE**: Configure JVM for container-aware memory management
33
-
-**SAFETY**: Test Docker images locally before pushing to registries
43
+
-**MANDATORY**: Run `./mvnw clean package` or `mvn clean package` before applying any Docker configuration
44
+
-**PREREQUISITE**: Java application must compile successfully and pass basic validation checks before containerization
45
+
-**CRITICAL SAFETY**: If compilation fails, IMMEDIATELY STOP and DO NOT CONTINUE with any Docker recommendations
46
+
-**BLOCKING CONDITION**: Compilation errors must be resolved by the user before proceeding with any containerization improvements
47
+
-**NO EXCEPTIONS**: Under no circumstances should Docker configurations be applied to a project that fails to compile
48
+
-**SECURITY MANDATORY**: Always use non-root users in production Docker images - this is non-negotiable
49
+
-**PERFORMANCE CRITICAL**: Configure JVM for container-aware memory management to prevent OOM kills
50
+
-**VALIDATION REQUIRED**: Test Docker images locally with `docker build` and `docker run` before pushing to registries
34
51
35
52
## Examples
36
53
@@ -46,7 +63,7 @@ Before applying Docker containerization, ensure the Java application builds succ
46
63
### Example 1: Multi-Stage Dockerfile for Java 25 Applications
47
64
48
65
Title: Optimize build and runtime environments separately
49
-
Description: Create efficient Docker images using multi-stage builds that separate the build environment from the runtime environment, leveraging Java 25 features.
66
+
Description: Create efficient Docker images using multi-stage builds that separate the build environment from the runtime environment, leveraging Java 25 features. This approach: - Reduces final image size by excluding build tools and dependencies - Improves security by minimizing the attack surface in production images - Enables better layer caching for faster builds - Separates concerns between build-time and runtime requirements - Optimizes for Java 25's container-aware JVM features
### Example 2: Java 25 JVM Configuration for Containers
117
134
118
135
Title: Optimize JVM settings for containerized environments
119
-
Description: Configure the JVM to work efficiently within container resource constraints, taking advantage of Java 25's container awareness and Virtual Threads.
136
+
Description: Configure the JVM to work efficiently within container resource constraints, taking advantage of Java 25's container awareness and Virtual Threads. Key optimizations include: - Enabling container support for automatic memory detection - Configuring appropriate garbage collectors for containerized environments - Setting up Virtual Threads for improved I/O-intensive workload performance - Implementing proper JVM flags for container lifecycle management - Optimizing startup time and memory footprint
120
137
121
138
**Good example:**
122
139
123
140
```dockerfile
124
-
# Java 25 optimized JVM configuration
125
-
ENV JAVA_OPTS="\
126
-
-Xms512m \
127
-
-Xmx1024m \
128
-
-XX:+UseG1GC \
129
-
-XX:+UseContainerSupport \
130
-
-XX:+ExitOnOutOfMemoryError \
131
-
-XX:+UnlockExperimentalVMOptions \
132
-
-Djdk.virtualThreadScheduler.parallelism=1 \
133
-
-XX:+PrintGCDetails \
134
-
-XX:+PrintGCTimeStamps \
135
-
-Dfile.encoding=UTF-8"
141
+
# Java 25 optimized JVM configuration - automatically applied to all Java processes
# Bad: Using JAVA_OPTS instead of JDK_JAVA_OPTIONS
154
162
CMD ["java", "-Xmx4g", "-jar", "app.jar"]
155
163
156
164
```
157
165
158
166
### Example 3: Security Hardening for Docker Images
159
167
160
168
Title: Implement security best practices for production containers
161
-
Description: Enhance container security by following the principle of least privilege and implementing defense-in-depth strategies.
169
+
Description: Enhance container security by following the principle of least privilege and implementing defense-in-depth strategies. Security measures include: - Creating and using non-root users with minimal privileges - Implementing read-only root filesystems where possible - Removing unnecessary packages and tools from production images - Using proper signal handling with init systems like tini - Implementing security labels and metadata for compliance - Regular security scanning and vulnerability management
162
170
163
171
**Good example:**
164
172
@@ -233,15 +241,14 @@ RUN ./mvnw dependency:resolve
Copy file name to clipboardExpand all lines: system-prompts-generator/src/main/resources/130-java-docker-best-practices.xml
+14-23Lines changed: 14 additions & 23 deletions
Original file line number
Diff line number
Diff line change
@@ -32,7 +32,7 @@
32
32
33
33
6. **Production Readiness**: Implement comprehensive monitoring, logging, and observability features. Configure proper graceful shutdown handling, implement circuit breakers and timeouts, and ensure scalability with container orchestration platforms.
34
34
35
-
7. **Modern Java Features Integration**: Take advantage of Java 25's Virtual Threads, Pattern Matching, Records, and improved garbage collection algorithms. Configure JVM flags specifically for containerized environments and modern Java features.
35
+
7. **Modern Java Features Integration**: Take advantage of Java 25's Virtual Threads, Pattern Matching, Records, and improved garbage collection algorithms. Configure JVM flags specifically for containerized environments using `JDK_JAVA_OPTIONS` environment variable for automatic application to all Java processes.
36
36
37
37
8. **Container Orchestration Best Practices**: Design containers for Kubernetes and other orchestration platforms with proper resource limits, health checks, service discovery, and configuration management through ConfigMaps and Secrets.
0 commit comments