Paketo.io / CloudNativeBuildPack created Container image doesn't have curl
installed - and this is for good reasons (see https://stackoverflow.com/a/65393843/4964553 & https://blog.sixeyed.com/docker-healthchecks-why-not-to-use-curl-or-iwr/).
So we need another aproach... As suggested we should use the container inherited language (here: Java/JDK) to write our own healthcheck.
So let's look at this great article https://mflash.dev/blog/2021/03/01/java-based-health-check-for-docker/ , where Naiyer explains how to write a simple healthcheck based on Java 11+
Here's my adoption leveraging an argument to provide a specific port. Just look into HealthCheck.java:
import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse.BodyHandlers;
/*
* Kudos to https://mflash.dev/blog/2021/03/01/java-based-health-check-for-docker/
*/
public class HealthCheck {
public static void main(String[] args) throws InterruptedException, IOException {
if(args.length == 0) {
System.out.println("Please append the App's port like: java HealthCheck.java 8098");
throw new RuntimeException("Argument port missing");
}
var request = HttpRequest.newBuilder()
.uri(URI.create("http://localhost:" + args[0] + "/actuator/health"))
.header("accept", "application/json")
.build();
var response = HttpClient.newHttpClient().send(request, BodyHandlers.ofString());
if (response.statusCode() != 200 || !response.body().contains("UP")) {
throw new RuntimeException("Healthcheck failed");
}
}
}