Run a Spring Boot REST API with Java 25 LTS, OpenAPI, and Kubernetes examples.
1.1K
Run a Spring Boot REST API with Java 25 LTS, OpenAPI, and Kubernetes examples.
Use this application to learn how to run a Java service in Docker or Amazon Elastic
Kubernetes Service (EKS). The container listens on port 8080.
| Tag | Meaning | Platform |
|---|---|---|
latest | Latest successful main build | linux/amd64 |
docker pull stden/java-eks:latest
docker run --rm --name docker-gems-java -p 127.0.0.1:8080:8080 stden/java-eks:latest
After the application starts, run this command in another terminal:
curl --fail http://localhost:8080/api/health
Open http://localhost:8080/swagger-ui/index.html to use the API documentation.
The working directory is /app. The image starts its application JAR with JAVA_OPTS.
Application source · Dockerfile · Docker tutorial · Build results
/api/health endpoint to check the applicationjava-eks/
│
├── src/main/java/com/example/demo/ # Application source
│ ├── controller/ # REST controllers
│ │ ├── HealthController.java # Health-check endpoint
│ │ └── ProductController.java # Product API
│ ├── model/ # Data models
│ │ └── Product.java # Product model
│ └── DemoApplication.java # Application entry point
│
├── src/main/resources/ # Application resources
│ └── application.yml # Spring Boot configuration
│
├── src/test/ # Tests
│ ├── java/... # Test classes
│ └── resources/application.yml # Test configuration
│
├── kubernetes/ # Kubernetes configuration
│ ├── kustomization.yaml # kubectl apply -k kubernetes
│ ├── namespace.yaml # Namespace eks-demo
│ ├── deployment.yaml # Deployment
│ └── service.yaml # Service and Ingress
│
├── .mvn/wrapper/, mvnw, mvnw.cmd # Maven Wrapper: downloads Maven 3.10.0
├── Dockerfile # Docker image build
├── pom.xml # Maven configuration
└── README.md # Project documentation
It is not necessary to install Maven. ./mvnw downloads the necessary version. On Windows, use mvnw.cmd.
If necessary, clone the project repository.
git clone https://github.com/stden/docker-gems.git
Open the project directory.
cd docker-gems/java-eks
Build the project with Maven.
./mvnw clean package
Start the application.
java -jar target/eks-demo-0.0.1-SNAPSHOT.jar
The application is available at http://localhost:8080.
Run the tests with a coverage report.
./mvnw clean test jacoco:report
Open the report at target/site/jacoco/index.html.
Check that the coverage meets the 95% minimum.
./mvnw clean verify
These steps do not use a JDK or Maven on the host.
Build the Docker image.
docker build -t stden/java-eks:latest .
Start the container.
docker run -p 8080:8080 stden/java-eks:latest
Publish the image only after its tests pass. GitHub Actions runs the tests and publishes
the image from main. To build, test, and publish it manually, run this command from
the java-eks directory:
../ci/publish-image.sh java-eks
test-images.sh runs
mvn -B verify with the 95% JaCoCo check in maven:3-amazoncorretto-25. Then it starts
the container and checks HTTP 200 from /api/health, /api-docs, and
/swagger-ui/index.html.
The AWS guide shows how to
copy the image to Amazon ECR. It also shows how to create an EKS cluster with Auto Mode. The
alb IngressClass from that guide is necessary for the Ingress.
Apply the namespace, the Deployment, the Service, and the Ingress.
kubectl apply -k kubernetes
Wait until both pods are ready.
kubectl -n eks-demo rollout status deployment/eks-demo
Check the pods, the Service, and the address of the Ingress.
kubectl -n eks-demo get pods,services,ingress
The pods run as a non-root user with a read-only root filesystem, and the namespace
enforces the restricted Pod Security Standard. The image platform is linux/amd64, so
the pods run only on amd64 nodes.
Use these endpoints after the application starts.
/swagger-ui.html/api-docscurl -X POST "http://localhost:8080/api/products" \
-H "Content-Type: application/json" \
-d '{
"name": "New product",
"description": "Product description",
"price": 299.99,
"category": "Electronics",
"stockQuantity": 10
}'
curl -X GET "http://localhost:8080/api/products"
JAVA_OPTS — JVM options (default: -XX:+UseContainerSupport -XX:MaxRAMPercentage=75.0 -XX:+UseG1GC -XX:+ExitOnOutOfMemoryError)SPRING_PROFILES_ACTIVE — the active Spring profile (for example dev, prod, or test)The kubernetes/ directory contains these files:
deployment.yaml defines the Deployment:
service.yaml defines the Service and the Ingress:
The project includes these tests and coverage checks:
Content type
Image
Digest
sha256:f4863f24b…
Size
256.7 MB
Last updated
1 day ago
docker pull stden/java-eks