Container builds

Build an image with a Containerfile and Buildah, run the same build on OpenShift, and use S2I when a builder image already knows how to compile.

Updated CompTech Team

The continuous integration lab stops at a tested commit. This lab turns that commit into an image and puts the image somewhere OpenShift can pull it. Three tools show up, and they overlap on purpose.

A Containerfile is the build recipe. Buildah reads a file named Containerfile or Dockerfile. The instructions are the same. Buildah runs those instructions, on your workstation and inside an OpenShift build pod. Source-to-image (S2I) is the path where you do not write a recipe: a builder image already contains the compile step and the run step. The JBoss Web Server lab is an S2I build. The EAP lab calls the same kind of assemble script from a Containerfile.

The registry in the middle is the OpenShift integrated registry. A push creates or updates an ImageStream tag. A Deployment that follows that tag rolls when the tag moves.

Prerequisites

  • Podman and Buildah (buildah version)
  • OpenShift 4.x and oc
  • The Inventory project from the Spring Boot lab, or any directory with the Containerfile below and a Maven project that packages target/inventory-1.0.0.jar

The cluster can pull registry.access.redhat.com.

A Containerfile

Save this as Containerfile next to pom.xml. It is the Dockerfile from the Spring Boot lab, under the other name:

FROM registry.access.redhat.com/ubi9/openjdk-21 AS build
WORKDIR /build
COPY --chown=185:0 pom.xml .
COPY --chown=185:0 src src
USER 185
RUN mvn -B -DskipTests package

FROM registry.access.redhat.com/ubi9/openjdk-21-runtime
COPY --from=build --chown=185:0 /build/target/inventory-1.0.0.jar /deployments/app.jar
EXPOSE 8080
ENV JAVA_APP_JAR=app.jar

.dockerignore:

target/

The first stage compiles. The second stage is the runtime image, which already listens on 8080 as user 185. Tests ran in CI. The image build skips them so a slow test suite does not run twice. Skipping tests here is safe only because main is already green. Do not skip them in the CI script.

Buildah on the workstation

buildah build -t inventory:1 -f Containerfile .
buildah images
podman run --rm -p 8080:8080 -e APP_API_KEY=dev-key inventory:1

buildah build and buildah bud are the same command. bud is the older name. The image is local until you push it.

Log in to the integrated registry and push. An administrator has to have exposed the registry route once (oc patch configs.imageregistry.operator.openshift.io/cluster --type=merge -p '{"spec":{"defaultRoute":true}}').

oc new-project inventory || oc project inventory
oc registry login
REG=$(oc registry info)
buildah push --tls-verify=false inventory:1 "$REG/inventory/inventory:1"
oc get imagestream inventory
oc describe imagestreamtag inventory:1

The push creates the ImageStream inventory and the tag 1. --tls-verify=false is for the registry route's default certificate. Inside the cluster the registry name is image-registry.openshift-image-registry.svc:5000, and pods pull that name with the project's pull secret. A Deployment uses the in-cluster name:

image-registry.openshift-image-registry.svc:5000/inventory/inventory:1

Pin a release to an immutable tag (:1, or the digest oc describe prints). latest moves, which is useful for a lab and a poor name for the image you rolled out last Tuesday.

The same build, inside the cluster

You do not need Buildah on the workstation for the day-to-day build. A docker-strategy BuildConfig runs Buildah in a build pod. OpenShift looks for Dockerfile unless you set the path.

oc new-build --binary --strategy=docker --name=inventory --dockerfile-path=Containerfile
oc start-build inventory --from-dir=. --follow
oc get builds
oc get imagestream inventory

The output tag is inventory:latest. The build pod pulls the UBI images, runs both stages, and pushes to the internal registry. This is the command the Spring Boot, Quarkus, and Open Liberty labs use.

To build from Git instead of an upload, point a BuildConfig at the repository. This replaces the --binary build of the same name. Delete bc/inventory first if you already created it:

oc new-build --name=inventory --strategy=docker --dockerfile-path=Containerfile \
  https://github.com/<owner>/<repo>.git

A webhook or a Pipeline then runs oc start-build inventory. The OpenShift Pipelines lab is the Pipeline. The Jenkins lab calls oc start-build from a stage.

S2I, when the builder already knows

S2I injects your source into a builder image and runs that image's assemble script. The result is a new image. You use it when the builder is the product, which it is for JBoss Web Server and for the UBI OpenJDK image's own S2I scripts.

Import the builder, then start a source-strategy build:

oc import-image openjdk-21 \
  --from=registry.access.redhat.com/ubi9/openjdk-21:latest --confirm
oc new-build --binary --strategy=source --name=inventory-s2i \
  --image-stream=openjdk-21:latest
oc start-build inventory-s2i --from-dir=. --follow
oc get imagestream inventory-s2i

The UBI OpenJDK builder compiles with Maven and runs the jar it finds. The output image is the builder plus your application, which is larger than the two-stage Containerfile. Prefer the Containerfile when you care about the runtime image. Prefer S2I when the vendor image's assemble script is the supported way to produce the server, as it is for Tomcat on JBoss Web Server.

A source strategy's output is still an ImageStreamTag. The Deployment does not care which strategy produced the tag.

Where tags live

Push target What you get
$REG/<project>/<name>:<tag> from Buildah An ImageStream tag in that project
oc start-build on a BuildConfig The BuildConfig's output ImageStreamTag, usually :latest
Another registry buildah tag to that host, buildah push, and a pull secret on the pod

oc image mirror copies a tag from one registry to another when you are filling a disconnected cluster. Mirror the UBI images and the application image. A Deployment that still points at registry.access.redhat.com will not start if the node cannot reach it.

Roll out tag 1 on the Deployment from the Spring Boot lab. The container name is inventory. Pods pull the in-cluster registry name, which is the same tag you pushed through the route:

oc set image deployment/inventory \
  inventory=image-registry.openshift-image-registry.svc:5000/inventory/inventory:1
oc rollout status deployment/inventory

If the Deployment uses the image-change annotation from the EAP lab, a new tag rolls it without oc set image.

Troubleshooting

Symptom Likely cause Fix
oc registry info fails The registry route is off An administrator sets defaultRoute: true on the image registry operator
buildah push certificate error The route uses the default router cert --tls-verify=false for the lab, or trust the cluster CA
Docker strategy cannot find the recipe The file is Containerfile and the path was not set --dockerfile-path=Containerfile
S2I build cannot find assemble The image is not a builder Import ubi9/openjdk-21 or the JWS image, not the runtime image
Pod ErrImagePull The Deployment names another project Match image-registry.../<project>/<name>:<tag> to oc project

What's next

Continuous integration is the test this image build skips. Jenkins and OpenShift Pipelines start the BuildConfig on every merge. DevSecOps scans the image you just pushed.