Container Image
Build container images with tools and best practices.

Clone

1
git clone https://github.com/saturnism/jvm-helloworld-by-example
2
cd jvm-helloworld-by-example/helloworld-springboot-tomcat
Copied!

Build

1
./mvnw package
Copied!

Enable API

Enable Container Registry API to push your container images to the Container Registry.
1
gcloud services enable containerregistry.googleapis.com
Copied!

Containerize

Typically, tutorials teach you how to write a Dockerfile to containerize a Java application. A Dockerfile can be error prone and it's hard to implement all the best practices. Rather than writing a Dockerfile, use tools such as Jib and Buildpacks to automatically create optimized container images.

Build and Push

Most tools can build and push directly into a container registry. In case of Jib, this step does not require a Docker daemon at all, and it can push changed layers directly into a remote registry. This is great for automated CI/CD pipelines.
Jib
Buildpack
Buildpack with Cloud Build
Spring Boot 2.3
Jib can containerize any Java application easily, without a Dockerfile nor docker installed. Jib will push the container image directly to the remote registry.
1
PROJECT_ID=$(gcloud config get-value project)
2
./mvnw compile com.google.cloud.tools:jib-maven-plugin:2.4.0:build \
3
-Dimage=gcr.io/${PROJECT_ID}/helloworld
Copied!
You can configure Jib Maven plugin or Jib Gradle plugin directly in the build file to run the Jib easier, such as ./mvnw jib:build.
Cloud Native Buildpacks can containerize applications written in different language without a Dockerfile. It does require docker installed.
  1. 1.
    Install Docker locally - see Get Docker documentation.
  2. 2.
    Install pack CLI - see Installing pack documentation
  3. 3.
    Build container with pack, and use --publish flag to push directly to the remote registry:
1
# Paketo Buildpack
2
PROJECT_ID=$(gcloud config get-value project)
3
pack build \
4
--builder gcr.io/paketo-buildpacks/builder:base \
5
--publish \
6
gcr.io/${PROJECT_ID}/helloworld
7
8
# GCP Buildpack
9
PROJECT_ID=$(gcloud config get-value project)
10
pack build \
11
--builder gcr.io/buildpacks/builder:v1 \
12
--publish \
13
gcr.io/${PROJECT_ID}/helloworld
Copied!
Paketo Buildpack will calculate the minimum memory needed to run the Spring Boot application. For this Hello World example, the minimum is 1GB of RAM.
Cloud Build has built-in Buildpack support, so you can build the container image in the remote Cloud Build environment:
1
# GCP Buildpack
2
PROJECT_ID=$(gcloud config get-value project)
3
gcloud alpha builds submit \
4
--pack image=gcr.io/${PROJECT_ID}/helloworld
5
6
# Paketo Buildpack
7
PROJECT_ID=$(gcloud config get-value project)
8
gcloud alpha builds submit \
9
--pack image=gcr.io/${PROJECT_ID}/helloworld,builder=gcr.io/paketo-buildpacks/builder:base
Copied!
Since Spring Boot 2.3+, you can build container using the Spring Boot plugin.
1
PROJECT_ID=$(gcloud config get-value project)
2
3
# Maven with Paketo Buildpack
4
./mvnw spring-boot:build-image \
5
-Dspring-boot.build-image.imageName=gcr.io/${PROJECT_ID}/helloworld
6
7
# Maven with GCP Buildpack
8
./mvnw spring-boot:build-image \
9
-Dspring-boot.build-image.imageName=gcr.io/${PROJECT_ID}/helloworld \
10
-Dspring-boot.build-image.builder=gcr.io/buildpacks/builder
11
12
# Gradle with Paketo Buildpack
13
./gradlew bootBuildImage --imageName=gcr.io/${PROJECT_ID}/helloworld
14
15
# Gradle with GCP Buildpack
16
./gradlew bootBuildImage --imageName=gcr.io/${PROJECT_ID}/helloworld \
17
--builder=gcr.io/buildpacks/builder
Copied!
After the image is built, push the docker image to Container Registry:
1
docker push gcr.io/${PROJECT_ID}/helloworld
Copied!
Paketo Buildpack will calculate the minimum memory needed to run the Spring Boot application. For this Hello World example, the minimum is 1GB of RAM.

Build Locally

If you are running a local Docker daemon and you do not want to push straight to a remote registry, then you can build container images without pushing:
Jib
Buildpack
Spring Boot 2.3
1
PROJECT_ID=$(gcloud config get-value project)
2
./mvnw compile com.google.cloud.tools:jib-maven-plugin:2.4.0:dockerBuild \
3
-Dimage=gcr.io/${PROJECT_ID}/helloworld
Copied!
1
# Paketo Buildpack
2
PROJECT_ID=$(gcloud config get-value project)
3
pack build \
4
--builder gcr.io/paketo-buildpacks/builder:base \
5
gcr.io/${PROJECT_ID}/helloworld
6
7
# GCP Buildpack
8
PROJECT_ID=$(gcloud config get-value project)
9
pack build \
10
--builder gcr.io/buildpacks/builder:v1 \
11
gcr.io/${PROJECT_ID}/helloworld
Copied!
1
PROJECT_ID=$(gcloud config get-value project)
2
3
# Maven with Paketo Buildpack
4
./mvnw spring-boot:build-image \
5
-Dspring-boot.build-image.imageName=gcr.io/${PROJECT_ID}/helloworld
6
7
# Maven with GCP Buildpack
8
./mvnw spring-boot:build-image \
9
-Dspring-boot.build-image.imageName=gcr.io/${PROJECT_ID}/helloworld \
10
-Dspring-boot.build-image.builder=gcr.io/buildpacks/builder
11
12
# Gradle with Paketo Buildpack
13
./gradlew bootBuildImage --imageName=gcr.io/${PROJECT_ID}/helloworld
14
15
# Gradle with GCP Buildpack
16
./gradlew bootBuildImage --imageName=gcr.io/${PROJECT_ID}/helloworld \
17
--builder=gcr.io/buildpacks/builder
Copied!

Run Locally

If you have Docker installed locally, you can run the docker container locally to ensure everything works. This command will run the container locally and forward localhost's port 8080 to the container instance's port 8080.
1
PROJECT_ID=$(gcloud config get-value project)
2
3
docker pull gcr.io/${PROJECT_ID}/helloworld
4
docker run -ti --rm -p 8080:8080 gcr.io/${PROJECT_ID}/helloworld
Copied!
The -ti flag means allocate a TTY, and expect interaction via STDIN. The --rm flag means delete the container completely upon exit.

Connect Locally

You can connect to the container that's running locally
1
curl localhost:8080
Copied!

Last modified 1yr ago