Docker
Docker is the packaging format that made “works on my machine” solvable: an image is a frozen filesystem plus metadata, and a container is a process isolated by Linux namespaces and cgroups. Most interviews stop at “what is a container?”; senior loops go into layers, caching and image size.
The 60-second answer
Section titled “The 60-second answer”An image is an immutable, layered artefact; a container is a process running from it, with its own filesystem view, network namespace and resource limits. Docker builds an image from a Dockerfile as a stack of layers, each cached and reused until an earlier layer changes. Because layers are immutable, order instructions from least to most frequently changing, use multi-stage builds to keep compilers out of the runtime image, run as non-root, and keep the image small. Containers are ephemeral, so state goes in volumes or, better, a managed service outside them.
Images, containers and layers
Section titled “Images, containers and layers”An image is a read-only template identified by a tag and a content digest. A container is a
writable thin layer on top of it, backed by a process. A layer is the result of one instruction
(RUN, COPY, ADD), content-addressed and shared between images on the same host.
Storage drivers such as overlay2 merge layers into one filesystem view. Deleting a file in a later
layer does not shrink the image — it still exists earlier, marked as deleted. That is why “I rm’d
it and the image is still huge” happens.
Build cache and instruction order
Section titled “Build cache and instruction order”The cache is invalidated from the first changed instruction onwards, so rarely-changing things go first:
FROM node:22-alpineWORKDIR /appCOPY package.json package-lock.json ./ # manifests change rarelyRUN npm ci --omit=devCOPY . . # source changes constantly, so lastUSER nodeCMD ["node", "server.js"]Copying . before npm ci would reinstall dependencies on every source edit — the most common
build-time mistake in real teams.
Dockerfile best practices
Section titled “Dockerfile best practices”- Pin the base image by minor version (
node:22-alpine, nevernode:latest); use a digest pin for hard reproducibility. - Combine related
RUNsteps and clean package caches in the same layer, e.g.apt-get update && apt-get install -y --no-install-recommends X && rm -rf /var/lib/apt/lists/*. - Use
.dockerignoreto keep.git,node_modules, build output and secrets out of the context; a large context slows every build.
Multi-stage builds
Section titled “Multi-stage builds”The compiler, test dependencies and source code do not belong in the runtime image:
FROM golang:1.23-alpine AS buildWORKDIR /srcCOPY . .RUN CGO_ENABLED=0 go build -o /out/app ./cmd/app
FROM gcr.io/distroless/static-debian12COPY --from=build /out/app /appUSER nonroot:nonrootENTRYPOINT ["/app"]Volumes, bind mounts and tmpfs
Section titled “Volumes, bind mounts and tmpfs”The container’s writable layer is the wrong place for data: it disappears with the container.
| Type | Where it lives | Typical use |
|---|---|---|
| Named volume | Docker-managed area on the host | Database data, persistent state |
| Bind mount | A host path you choose | Local development, config files |
| tmpfs | Memory only | Scratch space, secrets kept off disk |
Networking
Section titled “Networking”- bridge (default): internal IP, publish ports with
-p host:container. - user-defined bridge: adds DNS by container name — why Compose services reach each other by service name.
- host: no isolation, shares the host network. Fast, less portable.
- none: no networking, for batch jobs.
- overlay: multi-host networking, used by Swarm and Kubernetes-style orchestrators.
- macvlan: real MAC addresses on the LAN.
Compose
Section titled “Compose”Compose describes a multi-container local environment in one file; it is a dev tool, not an orchestrator.
services: web: build: . ports: ["3000:3000"] environment: { DATABASE_URL: postgres://app:secret@db:5432/app } depends_on: db: { condition: service_healthy } db: image: postgres:16-alpine environment: { POSTGRES_USER: app, POSTGRES_PASSWORD: secret } volumes: ["pgdata:/var/lib/postgresql/data"] healthcheck: { test: ["CMD-SHELL", "pg_isready -U app"], interval: 5s }
volumes: pgdata:depends_on with condition: service_healthy waits for readiness; plain depends_on only waits for
the container to start, which is not the same thing.
Follow-up questions
Section titled “Follow-up questions”- Why is the image still large after deleting a file in a later layer?
- How do you get dependency caching right in CI, where the daemon starts empty?
COPYversusADD— why preferCOPY?- How would you pass a secret to a build without baking it into a layer?
- Why does the container not shut down gracefully on
docker stop?
Common traps
Section titled “Common traps”CMDin shell form. The shell becomes PID 1,SIGTERMnever reaches your app, and shutdown waits for the timeout.:latestin production. No reproducibility, no rollback target.- Running as root. A container escape becomes a host problem.
- Data in the writable layer. It vanishes on restart.