Simple definition#
A container is a normal Linux process that the kernel isolates — it gets its own view of files, network and processes, and limited CPU and memory. A container image is a portable package of the application and everything it needs (runtime, libraries, config defaults), built in layers and stored in a registry.
Containers vs virtual machines#
flowchart TB
subgraph VM["Virtual machines"]
H1["Hardware"] --> HV["Hypervisor"]
HV --> G1["Guest OS + app A"]
HV --> G2["Guest OS + app B"]
end
subgraph CT["Containers"]
H2["Hardware"] --> OS["Host OS kernel"]
OS --> C1["Container: app A<br/>namespaces + cgroups"]
OS --> C2["Container: app B"]
end
| VM | Container | |
|---|---|---|
| Isolation | Strong (separate kernel) | Process-level (shared kernel) |
| Start time | Tens of seconds | Milliseconds to seconds |
| Size | GBs | MBs |
| Density | Lower | Higher |
How it works#
- Namespaces isolate what a process sees: PID, network, mount, UTS (hostname), IPC, user.
- cgroups limit what it can use: CPU, memory, I/O.
- Union file systems stack read-only image layers with a thin writable layer.
- seccomp, capabilities, SELinux/AppArmor restrict what it may do.
Images and OCI#
flowchart TB
L4["App JAR / binary — changes often"] --> L3["Dependencies"]
L3 --> L2["Runtime: JRE, Python"]
L2 --> L1["Base OS layer: distroless, UBI, Alpine"]
- Layers are cached and shared — order Dockerfile instructions from least to most frequently changed.
- The Open Container Initiative (OCI) standardises the image format, runtime and distribution, so images built with Docker, Buildah or Jib run on containerd, CRI-O or Podman.
- Registries (Docker Hub, GHCR, Quay, Harbor, ECR, ACR, Artifact Registry) also store Helm charts, SBOMs and signatures as OCI artifacts.
A good Dockerfile#
# ---- build stage ----
FROM eclipse-temurin:21-jdk AS build
WORKDIR /src
COPY mvnw pom.xml ./
COPY .mvn .mvn
RUN ./mvnw -q dependency:go-offline # cached unless pom.xml changes
COPY src src
RUN ./mvnw -q package -DskipTests
# ---- runtime stage ----
FROM gcr.io/distroless/java21-debian12:nonroot
WORKDIR /app
COPY --from=build /src/target/app.jar app.jar
USER nonroot
EXPOSE 8080
ENTRYPOINT ["java", "-XX:MaxRAMPercentage=75", "-jar", "app.jar"]
Best practices:
- Multi-stage builds — build tools stay out of the runtime image.
- Minimal, maintained base images — distroless, Chainguard, Docker Hardened Images (free since December 2025, with SBOMs and SLSA provenance), Red Hat UBI micro.
- Run as non-root; read-only root file system where possible.
- Pin base images by digest and rebuild regularly for patches.
- No secrets in images — use BuildKit secret mounts at build time and runtime secret injection.
.dockerignoreto keep the build context small.- One process per container; log to stdout/stderr.
- JVM in containers: container-aware memory flags (
MaxRAMPercentage); consider CDS/AOT or native images for start-up time.
Build tools#
| Tool | Notes |
|---|---|
| Docker + BuildKit / buildx | Default; multi-platform builds, cache export, SBOM and provenance attestations |
| Buildah / Podman | Daemonless, rootless; common on Red Hat platforms |
| Kaniko / BuildKit in CI | Build inside Kubernetes without a Docker daemon |
| Jib (Java) | Builds optimised layers without a Dockerfile |
| Cloud Native Buildpacks (Paketo) | Source to image with maintained base images; used by Spring Boot build-image |
Runtimes: Docker, containerd, Podman#
flowchart TB
CLI["docker CLI / podman / kubelet"] --> HL["High-level runtime<br/>containerd, CRI-O, Podman"]
HL --> LL["Low-level OCI runtime<br/>runc, crun"]
LL --> K["Linux kernel<br/>namespaces, cgroups"]
- Docker Engine uses containerd internally; great for developer machines and builds. Docker Desktop adds Compose, Kubernetes and dev tooling.
- Kubernetes talks to containerd or CRI-O via CRI. (Kubernetes 1.35 is the last release supporting containerd 1.x — move to containerd 2.)
- Podman is daemonless and rootless, Docker-CLI compatible.
Container security essentials#
- Scan images in CI and registries (Trivy, Grype, registry scanners); block critical CVEs with policy.
- Generate SBOMs, sign images (cosign), verify at admission — see Kubernetes security.
- Drop capabilities, non-root, seccomp; never
--privileged. - Keep images small and rebuild often.
Common mistakes#
Quick check#
What isolates a container? Linux namespaces (what it can see) and cgroups (what it can use), plus seccomp, capabilities and LSMs (what it may do).
Why use multi-stage builds? To keep compilers and build tools out of the runtime image, making it smaller and more secure.
What is OCI? The Open Container Initiative standards for image format, runtime and distribution, which let images run on any compliant runtime.
Docker vs containerd vs Podman? Docker is a developer platform built on containerd; containerd (and CRI-O) are runtimes used by Kubernetes via CRI; Podman is a daemonless, rootless Docker-compatible engine.
Why deploy by digest? A digest identifies exact image content, so deployments are reproducible and cannot be changed by re-tagging.
Summary#
- Containers are isolated processes; images are layered OCI packages.
- Multi-stage, minimal, non-root, pinned, secret-free images.
- Build with BuildKit, Buildah, Jib or Buildpacks; run on containerd/CRI-O.
- Scan, SBOM, sign and verify every image.