Skip to content
AI Engineer Guide.

concept · basics · intermediate · concept 18 of 24

Containers

0 of 24 complete

Why it matters

An application that works on one machine and fails on another is usually a dependency or filesystem difference, and a container removes that class of problem by shipping the filesystem with the process. The remaining problems are operational: images that rebuild differently, containers that exceed memory limits and get killed, and device access that requires explicit runtime configuration. Containers make the environment reproducible, not the deployment automatic.

Mental model

It is a lunchbox packed with everything the meal needs: the food, the cutlery and the instructions. The analogy breaks because a lunchbox is sealed, while a container shares the kitchen's stove and can be affected by other containers using it.

How it works

Mechanism, step by stepInteractive
  1. 1Start from a minimal, pinned base image and install only runtime dependencies, keeping build tools out of the final layer.
  2. 2Order layers from least to most volatile so dependency installation caches across code changes.
  3. 3Run as a non-root user, with a read-only filesystem where possible and no secrets baked into layers.
  4. 4Declare CPU, memory and device requirements, and set limits that match what the application was tested with.
  5. 5Add a health check that verifies the process can do real work, not just that the port is open.
  6. 6Handle termination signals so in-flight work drains before the runtime sends a kill.

Step 1 of 6

Start from a minimal, pinned base image and install only runtime dependencies, keeping build tools out of the final layer.

Step 1/6 · step 1

Figure. The mechanism in this page's own steps. The figure adds sequence, not new claims.

What to notice. A step can be read alone, but the order is the mechanism. The control lets you stop on any one.

Text version

The page's steps: 1. Start from a minimal, pinned base image and install only runtime dependencies, keeping bu… 2. Order layers from least to most volatile so dependency installation caches across code ch… 3. Run as a non-root user, with a read-only filesystem where possible and no secrets baked i… 4. Declare CPU, memory and device requirements, and set limits that match what the applicati… 5. Add a health check that verifies the process can do real work, not just that the port is … 6. Handle termination signals so in-flight work drains before the runtime sends a kill.

  1. Start from a minimal, pinned base image and install only runtime dependencies, keeping build tools out of the final layer.
  2. Order layers from least to most volatile so dependency installation caches across code changes.
  3. Run as a non-root user, with a read-only filesystem where possible and no secrets baked into layers.
  4. Declare CPU, memory and device requirements, and set limits that match what the application was tested with.
  5. Add a health check that verifies the process can do real work, not just that the port is open.
  6. Handle termination signals so in-flight work drains before the runtime sends a kill.

Trade-offs

ChoiceGood forCostFails when
Minimal base imageSmall attack surface and fast pullsDebugging tools are absentYou need to inspect a failing container in place
Full base imageFamiliar toolingLarge image and more vulnerabilitiesPull time dominates cold start
Container per processClear lifecycle and scalingMore objects to orchestrateThe workload is a tight set of co-located processes
Device passthroughHardware accelerationRuntime and driver couplingThe host driver version drifts
Trade-off mapInteractive

Good for

Small attack surface and fast pulls

Cost

Debugging tools are absent

Fails when

You need to inspect a failing container in place

  1. You need to inspect a failing container in place
  2. Pull time dominates cold start
  3. The workload is a tight set of co-located processes
  4. The host driver version drifts

Figure. Each choice with what it buys, what it costs, and the case where it loses.

What to notice. The fourth column is the decision: if the losing case describes your situation, the choice is wrong.

Text version

Four trade-offs: Minimal base image is good for Small attack surface and fast pulls, costs Debugging tools are absent, and fails when You need to inspect a failing container in place. Full base image is good for Familiar tooling, costs Large image and more vulnerabilities, and fails when Pull time dominates cold start. Container per process is good for Clear lifecycle and scaling, costs More objects to orchestrate, and fails when The workload is a tight set of co-located processes. Device passthrough is good for Hardware acceleration, costs Runtime and driver coupling, and fails when The host driver version drifts

Failure modes

  • Unpinned base image. A rebuild picks up a new base and behaviour changes without a code diff. Debug: compare the base image digest in the built image metadata against the one recorded at release.
  • Memory limit kill. The process is killed with an out-of-memory signal and restarts in a loop. Debug: chart container memory against the limit and check the termination reason.
  • Secrets in layers. A credential is copied during build and remains readable in the image history. Debug: scan image layers for known secret patterns and fail the build on a match.

In production

Track restart count, memory headroom and image pull time, and alert on restarts caused by resource limits. Keep image digests in the release record so a rollback restores exactly what ran before.

Common misconceptions

People think: Containers are lightweight virtual machines.

Actually: They share the host kernel and isolate processes, which is why the security boundary differs from a virtual machine.

People think: A container makes an application portable everywhere.

Actually: It makes the userspace reproducible; kernel version, architecture and device drivers still belong to the host.

Interview angle

Keep image, runtime and host separate, and cover pinning, layer order, limits and signal handling. Be precise about the shared kernel. Calling containers small virtual machines and stopping at the Dockerfile is the common error.

Sources