0
0
Spring Bootframework~30 mins

Health checks in Docker in Spring Boot - Mini Project: Build & Apply

Choose your learning style9 modes available
Health checks in Docker with Spring Boot
📖 Scenario: You are deploying a Spring Boot application inside a Docker container. To keep your app reliable, you want Docker to check if your app is healthy and ready to serve requests.This project will guide you to add a simple health check endpoint in your Spring Boot app and configure Docker to use it.
🎯 Goal: Build a Spring Boot app with a health check endpoint at /actuator/health and configure a Dockerfile with a HEALTHCHECK instruction that uses this endpoint.
📋 What You'll Learn
Create a Spring Boot application with the Actuator dependency
Enable the health endpoint at /actuator/health
Write a Dockerfile that builds the Spring Boot app image
Add a Docker HEALTHCHECK instruction that queries http://localhost:8080/actuator/health
💡 Why This Matters
🌍 Real World
Health checks help Docker and orchestration tools know if your app is running well and ready to serve users. This avoids downtime and improves reliability.
💼 Career
Understanding health checks is important for DevOps, backend development, and cloud deployment roles where containerized apps are common.
Progress0 / 4 steps
1
Add Actuator dependency and enable health endpoint
In your Spring Boot build.gradle or pom.xml, add the Actuator dependency. Then, in src/main/resources/application.properties, enable the health endpoint by adding management.endpoints.web.exposure.include=health.
Spring Boot
Need a hint?

The Actuator dependency provides the health endpoint. You must expose it explicitly in application.properties.

2
Create a Dockerfile to build the Spring Boot app image
Create a Dockerfile that uses openjdk:17-jdk-slim as the base image. Copy the built jar file named app.jar into the container and set the command to run it with java -jar /app.jar.
Spring Boot
Need a hint?

Use the official OpenJDK 17 slim image. Copy your jar to /app.jar and run it with java -jar.

3
Add a Docker HEALTHCHECK instruction
In the Dockerfile, add a HEALTHCHECK instruction that runs curl -f http://localhost:8080/actuator/health || exit 1 every 30 seconds with a timeout of 5 seconds.
Spring Boot
Need a hint?

The HEALTHCHECK runs curl to check the health endpoint. If curl fails, it exits with 1 to mark unhealthy.

4
Complete the Dockerfile with exposed port
In the Dockerfile, add an EXPOSE 8080 instruction so Docker knows the app listens on port 8080.
Spring Boot
Need a hint?

Expose port 8080 so Docker knows which port the app uses.