Testable application image contracts
A small example
FROM busybox:1.37WORKDIR /appCOPY report.sh .USER 10001:10001ENTRYPOINT ["sh", "/app/report.sh"]CMD ["summary"]A smoke test checks a small but meaningful path through the delivered artifact. This contract requires the default output report=summary, the alternative report=detail, an invalid argument with exit code 2, and a nonroot default user. The variation uses shell variables to capture stdout and status without discarding failure information; run each assertion as shown, and stop if a check fails. An exit code from Docker itself can differ from application failure (for instance a failure to start the container). A full release also needs dependency and digest review, resource and signal tests, health/readiness, relevant security controls, platform tests and rollback plans. Passing these local contract checks does not certify production readiness.
Try it in a local lab
Manual lab only; basic language familiarity is helpful, but every source file is supplied. Use a fresh authorized disposable Linux VM with blocked external egress, a local Engine and local Buildx docker driver. Check docker context show, docker version, docker buildx version and docker buildx ls. Verify the reviewed preloaded native base image named in the recipe, its digest, tool versions, inherited entrypoint, file permissions and required commands. Tags are mutable. Confirm all bm-v11 names and tags are unused. Stop if any input must be fetched or a builder is remote. --network none at build time constrains RUN, not all builder traffic. Runtime --network none leaves container loopback only. No public ports, credentials, host mounts or registry access. Execute separately, stop on unexpected failure, and dispose of the VM even if cleanup fails. All Docker results are predictions. Before contacting any Engine or builder, verify the effective endpoint of this exact command, including DOCKER_CONTEXT, DOCKER_HOST and builder overrides. Use only a reviewed fixture directory containing the listed inputs. Check the build exit status and record the loaded image ID before running; a failed build does not clear an older tag.
Open lab-fixtures/docker-application-packaging/07-image-contract/ in the extracted archive. The Dockerfile and these files are supplied. Work on a disposable copy, not the original archive.
#!/bin/shcase "$#:$1" in 1:summary) printf 'report=summary\n' ;; 1:detail) printf 'report=detail\n' ;; *) printf 'expected summary or detail\n' >&2; exit 2 ;;esacdocker build --load --network none --progress=plain -t bm-v11-image-contract:lab .docker run --rm --pull=never --network none bm-v11-image-contract:lab( set -eu output=$(docker run --rm --pull=never --network none bm-v11-image-contract:lab) test "$output" = 'report=summary' output=$(docker run --rm --pull=never --network none bm-v11-image-contract:lab detail) test "$output" = 'report=detail' rc=0 message=$(docker run --rm --pull=never --network none bm-v11-image-contract:lab invalid 2>&1) || rc=$? test "$rc" -eq 2 test "$message" = 'expected summary or detail' test "$(docker run --rm --pull=never --network none --entrypoint id bm-v11-image-contract:lab -u)" = 10001 printf 'contract checks passed\n')If the contract holds, the invalid case produces its usage message on stderr, captured with 2>&1 for the assertion, and the script prints contract checks passed. This checks combined output, not stream separation; the separate stderr contract still needs verification. A failed assertion must be investigated.
Confirm names and tag belong to this lab. For a service, stop it if still running and remove only the named container. Then remove only this image tag without forcing. If a command fails, inspect state before continuing; do not remove unrelated objects. Dispose of the VM rather than clearing a shared cache.
Common mistake
Do not check only that a container starts. A wrong output, unexpected success on invalid input or incorrect identity can make a release unsuitable.
Manual practice
Create a contract checklist for the Python service from this chapter: successful path, unknown path, nonroot identity, clean stop, and evidence required. Distinguish tests already shown from tests you still need to implement.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Testable application image contracts — Knowledge check
Apply the application packaging concept to a new scenario.
Which is a useful negative assertion?
Why capture stdout and exit status separately?
Do passing smoke tests establish production readiness?