The service must run the same way on every machine. You package it as a small, non-root image, scan it, push it to a registry, and run it from the registry by digest with the runtime limits a production platform would set.
A pinned Alpine base, a fixed unprivileged user (10001), no write access to the app, and a health check.
Lessons: Dockerfile Directives Part 1: Build the Image, Dockerfile Directives Part 2: Run It Right, Docker Lab: Build Your First Image and Run It
Trivy lists HIGH and CRITICAL vulnerabilities in the image, so you can decide before shipping.
Lessons: Container & Supply Chain Security
Start a local registry (registry:2.8.3), push the image, and pull it back by its digest.
Lessons: Artifact Management & Alternative CI/CD, Containers and Docker
Run it read-only, with all capabilities dropped, no privilege escalation, and memory, CPU, and process limits.
Lessons: Docker in Production: Data, Secrets, Limits, and Security, cgroups Internals and Resource Controls
| Tool | Version |
|---|---|
| docker | 24 or later |
| trivy | 0.50 or later |
| curl | any |
| jq | 1.6 or later |
Ports: 18082, 5001 on 127.0.0.1. Resources: About 300 MB of disk for images; Trivy caches its vulnerability database (about 1 GB) in ~/.cache/trivy.
What it creates (and the reset removes): containers cap-orders and cap-registry; images cap-orders:1.0.0 and localhost:5001/orders:1.0.0; ~/labs/capstones/container. Every name starts with cap- or lives under ~/labs/capstones, so nothing of yours is changed. The scripts stop with a clear message if a tool is missing or a port is taken.
One archive holds every capstone and the shared service. Read a script before you run it; the main ones are shown below.
mkdir -p ~/labs && cd ~/labs curl -fsSLO https://mohankrishnapodile.com/capstones/capstones-lab.tar.gz tar -xzf capstones-lab.tar.gz && cd capstones-lab ls
All in one, with the reset afterwards even if a step fails:
bash run.sh 02-container
Or one step at a time, to look around between them (add KEEP=1 before bash run.sh to keep it running after verify):
bash 02-container/setup.sh bash 02-container/verify.sh bash 02-container/reset.sh
This is a real run of run.sh 02-container, recorded when the page was built. Digests, timings, and random values differ on your machine; every check should still say PASS, and the last line should say all checks passed.
== 1. Build the image (pinned base, non-root user, health check) sha256:5738428a2fbd size 74041099 bytes, user 10001:10001 == 2. Scan it for known vulnerabilities (HIGH and CRITICAL) HIGH: 15 == 3. Push it to a local registry localhost:5001/orders@sha256:5738428a2fbd6a0f626059b883046206becc7f212bba67e8e95239bf863b0054 == 4. Run it from the registry, by digest, with limits and a read-only filesystem cap-orders is ready on http://127.0.0.1:18082 == Image and registry PASS the scan report exists PASS no CRITICAL vulnerabilities (gate) PASS the registry lists the orders repository PASS tag 1.0.0 is in the registry == Runtime PASS the container runs the registry image, by digest PASS the service answers through the published port PASS runs as user 10001, not root PASS the root filesystem is read-only PASS memory limited to 128 MiB PASS CPU limited to 0.5 PASS at most 64 processes PASS all Linux capabilities dropped PASS the image's health check reports healthy RESULT: all checks passed
02-container/expected-output.txt02-container/reset.sh02-container/setup.sh02-container/verify.shlib.sh (shared helpers)app/orders.py (the service)02-container/reset.sh#!/usr/bin/env bash
# Removes the capstone 2 containers, images, and folder. Trivy's vulnerability database cache stays
# in ~/.cache/trivy; delete that folder too if you need the space.
source "$(dirname "$0")/../lib.sh"
REG_PORT="${REG_PORT:-5001}"
docker rm -f cap-orders cap-registry > /dev/null 2>&1 || true
docker image rm -f cap-orders:1.0.0 "localhost:$REG_PORT/orders:1.0.0" > /dev/null 2>&1 || true
rm -rf "$HOME/labs/capstones/container"
echo "capstone 2 reset: containers cap-orders and cap-registry and the cap-orders images removed"02-container/setup.sh#!/usr/bin/env bash
# Capstone 2, container: application -> Dockerfile -> image scan -> registry -> runtime limits.
source "$(dirname "$0")/../lib.sh"
need docker trivy curl jq
work container
PORT="${PORT:-18082}"; REG_PORT="${REG_PORT:-5001}"
port_free "$PORT"; port_free "$REG_PORT"
cp "$APP_SRC/orders.py" "$APP_SRC/Dockerfile" "$WORK/"
cd "$WORK"
say "1. Build the image (pinned base, non-root user, health check)"
docker build -q -t cap-orders:1.0.0 --build-arg VERSION=1.0.0 . | cut -c1-19
docker image inspect cap-orders:1.0.0 --format 'size {{.Size}} bytes, user {{.Config.User}}'
say "2. Scan it for known vulnerabilities (HIGH and CRITICAL)"
trivy image --quiet --scanners vuln --severity HIGH,CRITICAL --format json -o scan.json cap-orders:1.0.0
jq -r '[.Results[]?.Vulnerabilities[]?] | group_by(.Severity) | map("\(.[0].Severity): \(length)") | if length == 0 then "no HIGH or CRITICAL vulnerabilities" else join(", ") end' scan.json
say "3. Push it to a local registry"
docker run -d --name cap-registry -p "127.0.0.1:$REG_PORT:5000" registry:2.8.3 > /dev/null
wait_for "http://127.0.0.1:$REG_PORT/v2/" 20
docker tag cap-orders:1.0.0 "localhost:$REG_PORT/orders:1.0.0"
docker push -q "localhost:$REG_PORT/orders:1.0.0" > /dev/null
docker image inspect "localhost:$REG_PORT/orders:1.0.0" --format '{{range .RepoDigests}}{{println .}}{{end}}' | grep "^localhost:$REG_PORT/orders@" | tee digest.txt
docker pull -q "$(cat digest.txt)" > /dev/null # fetch it back from the registry, by digest
say "4. Run it from the registry, by digest, with limits and a read-only filesystem"
docker run -d --name cap-orders -p "127.0.0.1:$PORT:8080" \
--read-only --cap-drop ALL --security-opt no-new-privileges \
--memory 128m --cpus 0.5 --pids-limit 64 \
"$(cat digest.txt)" > /dev/null
wait_for "http://127.0.0.1:$PORT/readyz" 30 && echo " cap-orders is ready on http://127.0.0.1:$PORT"02-container/verify.sh#!/usr/bin/env bash
# Checks capstone 2: the image is scanned and in the registry, and the container runs hardened.
source "$(dirname "$0")/../lib.sh"
need docker curl jq
work container
PORT="${PORT:-18082}"; REG_PORT="${REG_PORT:-5001}"
cd "$WORK"
say "Image and registry"
check "the scan report exists" test -s scan.json
crit=$(jq '[.Results[]?.Vulnerabilities[]? | select(.Severity == "CRITICAL")] | length' scan.json)
[ "$crit" -eq 0 ] && ok "no CRITICAL vulnerabilities (gate)" || bad "$crit CRITICAL vulnerabilities: rebuild on a patched base image"
check "the registry lists the orders repository" bash -c "curl -fsS http://127.0.0.1:$REG_PORT/v2/_catalog | jq -e '.repositories | index(\"orders\")'"
check "tag 1.0.0 is in the registry" bash -c "curl -fsS http://127.0.0.1:$REG_PORT/v2/orders/tags/list | jq -e '.tags | index(\"1.0.0\")'"
say "Runtime"
img=$(docker inspect cap-orders --format '{{.Config.Image}}')
case "$img" in "localhost:$REG_PORT/orders@sha256:"*) ok "the container runs the registry image, by digest" ;; *) bad "the container runs $img, not the registry digest" ;; esac
check "the service answers through the published port" curl -fsS "http://127.0.0.1:$PORT/healthz"
uid=$(docker exec cap-orders id -u); [ "$uid" = 10001 ] && ok "runs as user 10001, not root" || bad "runs as uid $uid"
if docker exec cap-orders sh -c 'touch /app/x' 2>/dev/null; then bad "the filesystem is writable"; else ok "the root filesystem is read-only"; fi
mem=$(docker inspect cap-orders --format '{{.HostConfig.Memory}}'); [ "$mem" = 134217728 ] && ok "memory limited to 128 MiB" || bad "memory limit is $mem"
cpu=$(docker inspect cap-orders --format '{{.HostConfig.NanoCpus}}'); [ "$cpu" = 500000000 ] && ok "CPU limited to 0.5" || bad "CPU limit is $cpu"
pids=$(docker inspect cap-orders --format '{{.HostConfig.PidsLimit}}'); [ "$pids" = 64 ] && ok "at most 64 processes" || bad "pids limit is $pids"
caps=$(docker inspect cap-orders --format '{{.HostConfig.CapDrop}}'); [ "$caps" = "[ALL]" ] && ok "all Linux capabilities dropped" || bad "capabilities dropped: $caps"
for _ in $(seq 20); do [ "$(docker inspect cap-orders --format '{{.State.Health.Status}}')" = healthy ] && break; sleep 1; done
check "the image's health check reports healthy" bash -c "[ \"\$(docker inspect cap-orders --format '{{.State.Health.Status}}')\" = healthy ]"
summary| Symptom | Cause | Fix |
|---|---|---|
| "port 5001 is in use" | Another registry, or an earlier run, uses it. | REG_PORT=5011 bash run.sh 02-container. |
| The Trivy step is slow the first time | It downloads its vulnerability database. | Wait; later runs use the cache. |
| "permission denied" from docker | Your user is not in the docker group. | sudo usermod -aG docker $USER, then log out and in again (or use sudo for this lab). |
Every check prints PASS or FAIL with a reason, and the scripts keep their logs in the work folder under ~/labs/capstones; read them before running again.
bash 02-container/reset.sh
It removes: containers cap-orders and cap-registry; images cap-orders:1.0.0 and localhost:5001/orders:1.0.0; ~/labs/capstones/container. Downloaded container images that are not named cap- stay in Docker's cache; remove them with docker image rm if you need the space.
| Provider | Equivalent |
|---|---|
| AWS | Push to Amazon ECR with image scanning on push; run on ECS Fargate with a read-only root filesystem and task CPU and memory limits. |
| Azure | Push to Azure Container Registry with Defender for Containers scanning; run on Azure Container Apps with CPU and memory limits. |
| Google Cloud | Push to Artifact Registry with vulnerability scanning; run on Cloud Run with CPU and memory limits. |
verify proves the build works. The rubric is for you, or a reviewer, to judge how well: aim for every row in Meets, then pick rows to take to Exceeds.
| Criterion | Meets | Exceeds |
|---|---|---|
| Image | Runs as a fixed non-root user from a pinned base. | Also multi-stage, with nothing in the final image the app does not need. |
| Scanning | You read the scan and can say which findings matter. | You fail the build on fixable CRITICAL findings, as capstone 5 does. |
| Immutability | You run the image by digest, not by tag. | You can show the digest that was tested is the digest that runs. |
| Runtime limits | Memory, CPU, and process limits are set and the filesystem is read-only. | You can explain what happens when each limit is reached. |
Then test what you know with the assessments.