Day 2: Container Images
Goal: You write a Dockerfile, build, tag, modify, export and push an image and start it in the cluster. · Duration: approx. 60 min · Prerequisite: Day 1
Briefing
CKAD: Application Design and Build (20 %) – “Define, build and modify container images”. In the exam there is usually a Dockerfile waiting for you; you are asked to change it, build it with podman or docker, tag it and save it as an archive or push it to a registry.
Dockerfile ──podman build──> notes-api:1.0 ──podman tag/push──> 172.16.0.126:30500
| | |
multi-stage podman save -o x.tar kubelet pulls image
Everything runs on cp1 in the folder
~/ckad/notes-api.
Drill 1: Get the source code
cd ~/ckad
git clone https://github.com/thomaszachmann/ckad-zum-anfassen
cp -r ckad-zum-anfassen/notes-api ~/ckad/notes-api
cd ~/ckad/notes-api
ls -AExpected:
.dockerignore go.mod go.sum main.go main_test.go metrics.go store.go
notes-api is a small Go API with in-memory storage. An
API key via NOTES_API_KEY is mandatory, the port comes from
PORT (default 8080). The .dockerignore keeps
.git, keys and .env files out of the build
context.
Save your progress? Sign up for free
Drill 2: Multi-stage Dockerfile
cat > Dockerfile <<'EOF'
# Stage 1: build
FROM docker.io/library/golang:1.27-alpine AS build
WORKDIR /src
COPY go.mod go.sum ./
RUN go mod download
COPY . .
ARG VERSION=dev
RUN CGO_ENABLED=0 go build -ldflags="-s -w -X main.version=${VERSION}" \
-o /out/notes-api .
# Stage 2: ship
FROM docker.io/library/alpine:3.23
COPY --from=build /out/notes-api /usr/local/bin/notes-api
USER 65532:65532
EXPOSE 8080
ENTRYPOINT ["notes-api"]
EOFWhy like this? Copy go.mod and go.sum
first: if only the code changes, the dependencies come from the cache.
The second stage contains only the binary – no compiler, no source code.
USER 65532 runs the process without root privileges.
Save your progress? Sign up for free
Drill 3: Build with a build argument
podman build -t notes-api:1.0 --build-arg VERSION=1.0 .
podman images notes-apiExpected (shortened, the first run takes a good minute):
[1/2] STEP 1/7: FROM docker.io/library/golang:1.27-alpine AS build
...
[2/2] STEP 5/5: ENTRYPOINT ["notes-api"]
[2/2] COMMIT notes-api:1.0
Successfully tagged localhost/notes-api:1.0
REPOSITORY TAG IMAGE ID CREATED SIZE
localhost/notes-api 1.0 41ac32fa82fa 11 seconds ago 20.5 MB
podman prefixes names without a registry with
localhost/. The build image with Go (473 MB) remains behind
as <none>; only 20.5 MB get shipped.
Save your progress? Sign up for free
Drill 4: Test locally
podman run --rm notes-api:1.0
echo $?Expected:
time=2026-11-18T15:34:04.733Z level=ERROR msg="start abgebrochen"
err="NOTES_API_KEY oder NOTES_API_KEY_FILE fehlt"
1
With an API key (lab-key-123 is a lab value only):
podman run -d --name notes -p 8080:8080 -e NOTES_API_KEY=lab-key-123 notes-api:1.0
curl -s localhost:8080/version
curl -s -X POST -H 'X-API-Key: lab-key-123' -d '{"text":"hallo"}' localhost:8080/notes
podman exec notes id
podman rm -f notesExpected:
e6dfdb539f4d07bbfa233fa1511026db35da1b9cd848bbff938ffddb487d2c5b
1.0
{"id":"2c796fb5-34b9-4017-a42f-bbf41be949fe","text":"hallo"}
uid=65532(65532) gid=65532(65532)
notes
Save your progress? Sign up for free
Drill 5: Tag and push
An image can have any number of names. The name determines where
push goes:
podman tag notes-api:1.0 172.16.0.126:30500/notes-api:1.0
podman push 172.16.0.126:30500/notes-api:1.0Expected (shortened):
Error: trying to reuse blob sha256:7900...: pinging container registry
172.16.0.126:30500: Get "https://172.16.0.126:30500/v2/": http: server gave HTTP
response to HTTPS client
The lab registry only speaks HTTP:
podman push --tls-verify=false 172.16.0.126:30500/notes-api:1.0
curl -s http://172.16.0.126:30500/v2/notes-api/tags/listExpected:
Getting image source signatures
Copying blob sha256:9cff15c0e80c...
Copying blob sha256:7900194cf242...
Copying config sha256:41ac32fa82fa...
Writing manifest to image destination
{"name":"notes-api","tags":["1.0"]}
Save your progress? Sign up for free
Drill 6: Modify the image
Version 1.1 gets metadata and a default value for PORT.
In Dockerfile, replace the line
FROM docker.io/library/alpine:3.23 with:
FROM docker.io/library/alpine:3.23
ARG VERSION=dev
LABEL org.opencontainers.image.title="notes-api" \
org.opencontainers.image.version="${VERSION}"
ENV PORT=8080An ARG only applies in the stage where it is declared –
that is why it appears again in stage 2.
podman build -t notes-api:1.1 --build-arg VERSION=1.1 .
podman inspect notes-api:1.1 --format '{{.Config.Labels}}'
podman inspect notes-api:1.1 --format '{{.Config.User}} {{.Config.Env}}'
podman history notes-api:1.1 --format '{{.CreatedBy}} {{.Size}}' | head -6Expected (shortened):
Successfully tagged localhost/notes-api:1.1
map[io.buildah.version:1.33.7 org.opencontainers.image.title:notes-api
org.opencontainers.image.version:1.1]
65532:65532 [PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
PORT=8080]
/bin/sh -c #(nop) ENTRYPOINT ["notes-api"] 0B
/bin/sh -c #(nop) EXPOSE 8080 0B
/bin/sh -c #(nop) USER 65532:65532 0B
/bin/sh -c #(nop) COPY file:009b539941a4d8... 11.7MB
/bin/sh -c #(nop) ENV PORT=8080 0B
/bin/sh -c #(nop) LABEL org.opencontainers... 0B
The Go stage came entirely from the cache because nothing changed there.
podman tag notes-api:1.1 172.16.0.126:30500/notes-api:1.1
podman push --tls-verify=false 172.16.0.126:30500/notes-api:1.1
curl -s http://172.16.0.126:30500/v2/notes-api/tags/listExpected (shortened):
Writing manifest to image destination
{"name":"notes-api","tags":["1.0","1.1"]}
Save your progress? Sign up for free
Drill 7: The image as an archive
An exam classic: “Save the image as a tarball under …”. The default
is the Docker format; with --format oci-archive you get the
OCI format:
mkdir -p ~/ckad/tag02 && cd ~/ckad/tag02
podman save -o notes-api-1.1.tar notes-api:1.1
podman save --format oci-archive -o notes-api-1.1-oci.tar notes-api:1.1
ls -lh *.tar
tar tf notes-api-1.1-oci.tar | tail -2Expected (shortened):
-rw-r--r-- 1 ubuntu ubuntu 8.2M Nov 18 15:35 notes-api-1.1-oci.tar
-rw-r--r-- 1 ubuntu ubuntu 20M Nov 18 15:35 notes-api-1.1.tar
index.json
oci-layout
The OCI archive is smaller because it compresses the layers. Load it back:
podman rmi localhost/notes-api:1.1
podman load -i notes-api-1.1-oci.tarExpected (shortened):
Untagged: localhost/notes-api:1.1
...
Loaded image: localhost/notes-api:1.1
Save your progress? Sign up for free
Drill 8: Start it in the cluster
k create ns tag02
k config set-context --current --namespace=tag02
k run notes --image=172.16.0.126:30500/notes-api:1.0 --port=8080
k get pod notes
k logs notesExpected:
NAME READY STATUS RESTARTS AGE
notes 0/1 Error 2 (22s ago) 25s
time=2026-11-18T15:35:55.962Z level=ERROR msg="start abgebrochen"
err="NOTES_API_KEY oder NOTES_API_KEY_FILE fehlt"
The pull worked, the application aborts. Recreate it with an environment variable:
k delete pod notes $now
k run notes --image=172.16.0.126:30500/notes-api:1.0 --port=8080 \
--env=NOTES_API_KEY=lab-key-123
k get pod notes -o wide
curl -s $(k get pod notes -o jsonpath='{.status.podIP}'):8080/versionExpected (shortened):
pod/notes created
NAME READY STATUS RESTARTS AGE IP NODE
notes 1/1 Running 0 2s 192.168.235.186 worker1
1.0
The image is one of the few mutable pod fields:
k set image pod/notes notes=172.16.0.126:30500/notes-api:1.1
k exec notes -- wget -qO- localhost:8080/version
k exec notes -- idExpected:
pod/notes image updated
1.1
uid=65532 gid=0(root) groups=0(root)
Why gid=0? Kubernetes only takes the UID from
USER; the group is set only by runAsGroup in
the SecurityContext (Day 8).
Save your progress? Sign up for free
Exercise: Speed Drills
- Show the user and entrypoint of the image
notes-api:1.0. Target time: 30 s - Save
notes-api:1.0in OCI format as~/ckad/tag02/notes-api-1.0.tar. Target time: 30 s - List all tags of
notes-apiin the lab registry. Target time: 20 s
Solution
podman inspect notes-api:1.0 --format '{{.Config.User}} {{.Config.Entrypoint}}'
podman save --format oci-archive -o ~/ckad/tag02/notes-api-1.0.tar notes-api:1.0
curl -s http://172.16.0.126:30500/v2/notes-api/tags/listExpected (shortened):
65532:65532 [notes-api]
Writing manifest to image destination
{"name":"notes-api","tags":["1.0","1.1"]}
Save your progress? Sign up for free
Checkpoint
curl -s http://172.16.0.126:30500/v2/notes-api/tags/listExpected:
{"name":"notes-api","tags":["1.0","1.1"]} – later days need
notes-api:1.0 in the registry.
k get pod notes -o jsonpath='{.status.containerStatuses[0].imageID}'Expected:
172.16.0.126:30500/notes-api@sha256:6a78ccd2... (your
digest will differ)
ls ~/ckad/tag02/*.tarExpected: three archives, including
notes-api-1.0.tar.
Challenge
Start notes-api:1.1 as pod notes-9090
listening on port 9090, and check /healthz from a throwaway
pod.
Hint: An environment variable in the pod overrides
the ENV from the image.
Solution
k run notes-9090 --image=172.16.0.126:30500/notes-api:1.1 --port=9090 \
--env=NOTES_API_KEY=lab-key-123 --env=PORT=9090
IP=$(k get pod notes-9090 -o jsonpath='{.status.podIP}')
k run t --rm -i --image=busybox:1.37 --restart=Never -- wget -qO- -T 5 $IP:9090/healthz
k logs notes-9090 | tail -1Expected (shortened):
ok
pod "t" deleted from tag02 namespace
time=2026-11-18T15:36:42.346Z level=INFO msg=listening addr=:9090 version=1.1 pid=1
Clean up (the images in the registry stay):
k config set-context --current --namespace=default
k delete ns tag02Save your progress? Sign up for free
Exam Tip
- Read the task’s Dockerfile carefully: often only an
ARGorENVis missing, or a path inCOPY --fromis wrong. Build with-t name:tag, and do not forget the dot at the end. - “Save as a tarball/archive” means
podman save -o datei.tar(ordocker save); if the task says “OCI”, add--format oci-archive. podman image inspectandpodman historyanswer questions about user, ports, labels and layers without the docs.
Enterprise Note
- CI builds the images (Buildah, Kaniko, BuildKit) and signs them (cosign); nobody pushes from a laptop.
- Tags are mutable – in production, deploy by digest
(
@sha256:…) or enforce immutable tags in the registry. - Scan base images regularly (Trivy, Grype) and rebuild them;
distroless or
scratchshrink the attack surface further.