🐳Build Container Container Image Labs

🎯 Objective

Learn different approaches on how to build Docker containers like Dockerfile, Google JIB and Buildpacks using a simple Java Spring Boot App.


🧰 Prerequisites

Intallation

Install Httpiearrow-up-right as Http Client (more convenient as just curl):

sudo apt install httpie

πŸ”Ή Lab 1: Build an Run Spring Boot App

Step 1: Checkout from GitHub

Clone the corresponding GitHub repository using:

βœ… Expected: You have a new folder called kubernetes-sample-app.

Step 2: Build and Run the Spring Boot App

To build the Spring Boot App, change into the folder kubernetes-sample-app and then run:

If the build finished successfully you can then run the application by:

Open another terminal and try to access the provided APIs of this application:

You can find all provided endpoints documented here: API Endpointsarrow-up-right

Step 3: Stop the Spring Boot App

To stop the Spring Boot App, in your first terminal just hit Ctrl-C.


πŸ”Ή Lab 2: Build Containers Image using Dockerfile

Step 1: Inspect the provided Dockerfile

Have a look into the provided Dockerfile located in folder docker/secure using:

The Dockerfile uses a base image with a Java 21 JRE and follows best security practice to run with a non-root user (UID 1002):

Step 2: Build Container Image using Dockerfile

Step 3: Run the Container Image (Build with Dockerfile)

In the second terminal try again if the application is really running by calling:

βœ… Expected: You run the application as a container.

Step 4: Stop the Container Image (Build with Dockerfile)

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


πŸ”Ή Lab 3: Build Containers Image using Google JIB

Step 1: Inspect the Maven pom.xml for JIB plugin

Have a look into the provided Maven build file located in folder pom.xml using:

The pom.xml file contains a plugin for the Google JIB plugin to build a container image based on Google Distrolessarrow-up-right base image:

As you can see it uses gcr.io/distroless/java17-debian12 as base image and also runs with a non-root user (UID 1002).

Step 2: Build Container Image using JIB

After build has finished you may have a look inside the image using

E.g. look for jib as a proof that this is really the image build by JIB.

Step 3: Run the Container Image (Build with JIB)

As before just run this container again using:

In the second terminal try again if the application is really running by calling:

βœ… Expected: You run the application as a container.

Step 4: Stop the Container Image (Build with JIB)

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


πŸ”Ή Lab 4: Build Containers Image using Buildpack (Paketo)

Step 1: Build Container Image using Buildpack

Cloud Native Buildpacks (The standardization)arrow-up-right and Paketo (one implementation)arrow-up-right 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:

After build has finished you may have a look inside the image using

E.g. look for buildpack as a proof that this is really the image build by Spring Boot and Paketo Buildpacks.

Step 3: Run the Container Image (Build with Buildpack)

As before just run this container again using:

In the second terminal try again if the application is really running by calling:

βœ… Expected: You run the application as a container.

Step 4: Stop the Container Image (Build with Buildpack)

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


βœ… Wrap-Up

  • βœ… 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


Last updated