Container packaging
What you will learn
A Java container image packages a runtime and an application artifact with an explicit launch command. A multi-stage build compiles in a JDK stage and copies only the required output to a runtime stage. The container is a process isolation mechanism, not a proof of hostile-code containment.
- Separate compile and runtime artifacts.
- Distinguish image tags from immutable digest references.
- Validate runtime configuration before accepting traffic.
Plain Java examples: save as Main.java in a fresh directory, then run javac --release 21 Main.java and java Main. Standalone Java 21 functional examples. Docker and Kubernetes recipes are source-reviewed, not executed. Companion local HTTP/JDK evidence is separate.
Read a runtime port
public class Main { static int port(String value) { int n=Integer.parseInt(value); if(n<1024 || n>65535) throw new IllegalArgumentException("port"); return n; } public static void main(String[] args) { System.out.println(port("8080")); }}Expected output:
8080Configuration is validated before startup; the sample value is a string as environment variables are.
Build inputs and runtime ownership
The companion Dockerfile accepts required build arguments BUILD_IMAGE and RUN_IMAGE. Supply approved, platform-compatible JDK 21 and runtime 21 image references pinned to verified digests; no registry resolution was performed. The compile stage uses javac --release 21 and jar. The runtime stage copies the JAR, sets numeric USER 10001:10001 and uses a JSON-array ENTRYPOINT so Java is the direct process. A tag may change; a digest identifies content but does not establish trust. Review signatures, provenance, SBOM and patch status. Rebuild periodically with reviewed updated digests.
Configuration and least privilege
Use a nonprivileged port, read-only root filesystem where compatible, bounded writable temporary storage, no host socket, minimal privileges and no embedded secrets. Configuration supplied at runtime is still untrusted input. A Kubernetes Secret is not automatically encrypted at rest; use approved secret storage and access policies. Do not print credentials. The example checks port ranges, not whether a socket is available.
Distinguish a pinned reference shape
public class Main { static boolean pinned(String image) { return image.matches("[^@\\s]+@sha256:[0-9a-f]{64}"); } public static void main(String[] args) { System.out.println(pinned("catalog:demo")); System.out.println(pinned("catalog@sha256:"+"a".repeat(64))); }}Expected output:
falsetrueThe repeated a is synthetic; this validates syntax only, not existence or provenance.
Keep artifact identity separate from config
public class Main { record Release(String artifact,int port) {} public static void main(String[] args) { var a=new Release("catalog-v1",8080); var b=new Release("catalog-v1",8081); System.out.println(a.artifact().equals(b.artifact())); }}Expected output:
trueChanging a port does not change the application artifact identity.
Common mistakes
Do not copy the entire build directory into runtime, use a mutable latest tag as rollback identity, or bake passwords into Docker ARG/ENV. Shell entrypoints may fail to forward termination signals. Exec form helps direct delivery but does not itself implement cleanup.
Practice
Validate the port 9090 and print it. Reject ports below 1024 and above 65535 for this lab.
Expected output:
9090Compare with the manual review criteria.
Further reading: Docker stages · Dockerfile. Independent editorial review remains pending.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Container packaging — knowledge check
Container deployment and operations reasoning.
What is copied into the runtime stage?
What does an image digest establish?
Why use an exec-form entrypoint?
What should remain outside the image?
What does a numeric USER do?