Secure Container Image
While Creating a new container image is fairly easy and straightforward for development purposes, you should consider building a secure container image for production use. Below are some basic considerations.

No Source Code

It's a common mistake to copy too much data/information into a container image. In general, you should only have the content that's absolutely necessary to run your application. But there are things that often make it into a container image that you may not realize:
  • Source code, build files are easily copied into a runtime container image by accident when using a Dockerfile.
  • Version control directories, such as .git are easily copied into a runtime container image by accident when using a Dockerfile.
Jib
Buildpacks
Jib automatically builds thin container images without the source.
Paketo automatically builds thin container images without the source.
GCP Buildpack needs to set GOOGLE_CLEAR_SOURCE=true to remove the source from the container image. See GCP Buildpack README for more information.
1
PROJECT_ID=$(gcloud config get-value project) ⬢ system ⎈ demo-cluster
2
pack build \
3
-e "GOOGLE_CLEAR_SOURCE=true"
4
--builder gcr.io/buildpacks/builder:v1 \
5
--publish \
6
gcr.io/${PROJECT_ID}/helloworld
Copied!

No Secrets/Credentials

Do not copy secrets and/or credentials into a container image (e.g., do not copy a service account key file!). For the most part, secrets can be stored in the runtime environment (e.g., a Kubernetes Secret), or better, a secret store (e.g., Cloud Secret Manager, or HashiCorp Vault).

Minimal Base Image

Many base images comes with all the command line utilities from a typical Linux distribution (e.g., a shell, package manager, etc). These container images may allow you (or an attacker!) to get into a shell, and install additional tools. To reduce the attack surface, consider using a minimal base image that has the least attack surface. These images will be more secure, but may also be harder to debug.
Jib
Buildpacks
Jib uses the Distroless base image by default.
Buildpack's runtime image ultimately does not use Distroless. Paketo, for example, executed shell script to calculate memory needs, and thus Shell is needed. There is no easy way to switch out the base image when using a Buildpack. You may need to create your own to change the base image.

Non-Root User

One of the most overlooked configuration for a container image is which user is used to run your application? In a VM environment, you would never want to run an application as root. It's no different in a container. Every container image may have a different set of non-privileged users.
For example, for a Distroless base image (using a debug image that has a shell):
1
docker run -ti --rm --entrypoint=sh \
2
gcr.io/distroless/java:debug -c "cat /etc/passwd"
Copied!
You'll see that it has only 3 users:
1
root:x:0:0:root:/root:/sbin/nologin
2
nobody:x:65534:65534:nobody:/nonexistent:/sbin/nologin
3
nonroot:x:65532:65532:nonroot:/home/nonroot:/sbin/nologin
Copied!
But, an AdoptOpenJDK base image has more system users, and you'll need to pick the one you want to use as the user to run your application:
1
docker run -ti --rm adoptopenjdk:11-jre-hotspot-bionic cat /etc/passwd
Copied!
Jib
Buildpacks
Jib uses root user by default. You should configure it to use a non-root user according to the base image you use. For example:
1
PROJECT_ID=$(gcloud config get-value project)
2
./mvnw compile com.google.cloud.tools:jib-maven-plugin:2.4.0:build \
3
-Djib.container.user=nonroot:nonroot
4
-Dimage=gcr.io/${PROJECT_ID}/helloworld
Copied!
Validate that the JVM was started with the nonroot user:
1
PROJECT_ID=$(gcloud config get-value project)
2
3
docker pull gcr.io/${PROJECT_ID}/helloworld
4
docker run -ti --rm --entrypoint=java \
5
gcr.io/${PROJECT_ID}/helloworld \
6
-XshowSettings:properties -version
Copied!
Look for the user.name property is now nonroot.
Buildpacks (with Paketo and GCP builders) run as a non-root user by default, as the user cnb.

Summary

So, what do the automated tools do by default?
Jib
Paketo Builder
GCP Builder
Source Code
No source in runtime
No source in runtime
Set GOOGLE_CLEAR_SOURCE.
See README.
Minimal Base Image
Uses Distroless
Not Distroless
Not Distroless
Non-Root User
Defaults to root,
Configure to non-root.
cnb user
cnb user