Learn different approaches on how to build Docker containers like Dockerfile, Google JIB and Buildpacks using a simple Java Spring Boot App.
- Docker installed
- Java SDK (at least 17 LTS) installed
- Http Client installed (Httpie)
Install Httpie as Http Client (more convenient as just curl):
sudo apt install httpieClone the corresponding GitHub repository using:
git clone https://github.com/andifalk/kubernetes-sample-app.git✅ Expected: You have a new folder called kubernetes-sample-app.
To build the Spring Boot App, change into the folder kubernetes-sample-app and then run:
./mvnw clean packageIf the build finished successfully you can then run the application by:
./mvnw spring-boot:runOpen another terminal and try to access the provided APIs of this application:
http localhost:8080/api/helloYou can find all provided endpoints documented here: API Endpoints
To stop the Spring Boot App, in your first terminal just hit Ctrl-C.
Have a look into the provided Dockerfile located in folder docker/secure using:
cat docker/secure/DockerfileThe Dockerfile uses a base image with a Java 21 JRE and follows best security practice to run with a non-root user (UID 1002):
FROM bellsoft/liberica-openjre-alpine:21.0.6
ARG TARGETPLATFORM
RUN echo "I'm building for $TARGETPLATFORM"
COPY target/kubernetes-sample-app-1.0.0-SNAPSHOT.jar app.jar
EXPOSE 8080
RUN addgroup -S app -g 1002 && adduser -S appuser --u 1002 --g 1002
USER 1002
ENTRYPOINT ["java", "-jar", "/app.jar"]docker build -f docker/secure/Dockerfile -t kubernetes-sample-app .docker run -p 8080:8080 kubernetes-sample-appIn the second terminal try again if the application is really running by calling:
http localhost:8080/api/hello✅ Expected: You run the application as a container.
To stop the docker container from running just again type Ctrl-C in your first terminal.
To make sure no container is running you always can just perform
docker psHave a look into the provided Maven build file located in folder pom.xml using:
cat pom.xmlThe pom.xml file contains a plugin for the Google JIB plugin to build a container image based on Google Distroless base image:
<plugin>
<groupId>com.google.cloud.tools</groupId>
<artifactId>jib-maven-plugin</artifactId>
<version>3.4.5</version>
<configuration>
<to>
<image>andifalk/kubernetes-sample-app:jib</image>
</to>
<from>
<image>gcr.io/distroless/java17-debian12</image>
<platforms>
<platform>
<architecture>amd64</architecture>
<os>linux</os>
</platform>
<platform>
<architecture>arm64</architecture>
<os>linux</os>
</platform>
</platforms>
</from>
<container>
<user>1002</user>
</container>
</configuration>
</plugin>As you can see it uses gcr.io/distroless/java17-debian12 as base image and also runs with a non-root user (UID 1002).
./mvnw jib:dockerBuild -Dimage=kubernetes-sample-appAfter build has finished you may have a look inside the image using
docker inspect kubernetes-sample-appE.g. look for jib as a proof that this is really the image build by JIB.
As before just run this container again using:
docker run -p 8080:8080 kubernetes-sample-appIn the second terminal try again if the application is really running by calling:
http localhost:8080/api/hello✅ Expected: You run the application as a container.
To stop the docker container from running just again type Ctrl-C in your first terminal.
To make sure no container is running you always can just perform
docker psCloud Native Buildpacks (The standardization) and Paketo (one implementation) are the default mechanism in Spring Boot to build container images using the Spring Boot Maven plugin.
So to build the container image just perform:
./mvnw spring-boot:build-image -Dspring-boot.build-image.imageName=kubernetes-sample-appAfter build has finished you may have a look inside the image using
docker inspect kubernetes-sample-appE.g. look for buildpack as a proof that this is really the image build by Spring Boot and Paketo Buildpacks.
As before just run this container again using:
docker run -p 8080:8080 kubernetes-sample-appIn the second terminal try again if the application is really running by calling:
http localhost:8080/api/hello✅ Expected: You run the application as a container.
To stop the docker container from running just again type Ctrl-C in your first terminal.
To make sure no container is running you always can just perform
docker ps- ✅ Different approaches to build container images
- ✅ Run as non-root
- ✅ Using Dockerfile
- ✅ Using Google JIB and Distroless
- ✅ Using Spring Boot and Paketo Buildpack
- ✅ Run the Container and test the API provided by the application