Skip to content

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.

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.

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.

The cache is invalidated from the first changed instruction onwards, so rarely-changing things go first:

Dockerfile
FROM node:22-alpine
WORKDIR /app
COPY package.json package-lock.json ./ # manifests change rarely
RUN npm ci --omit=dev
COPY . . # source changes constantly, so last
USER node
CMD ["node", "server.js"]

Copying . before npm ci would reinstall dependencies on every source edit — the most common build-time mistake in real teams.

  • Pin the base image by minor version (node:22-alpine, never node:latest); use a digest pin for hard reproducibility.
  • Combine related RUN steps 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 .dockerignore to keep .git, node_modules, build output and secrets out of the context; a large context slows every build.

The compiler, test dependencies and source code do not belong in the runtime image:

multi-stage Go build
FROM golang:1.23-alpine AS build
WORKDIR /src
COPY . .
RUN CGO_ENABLED=0 go build -o /out/app ./cmd/app
FROM gcr.io/distroless/static-debian12
COPY --from=build /out/app /app
USER nonroot:nonroot
ENTRYPOINT ["/app"]

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
  • 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 describes a multi-container local environment in one file; it is a dev tool, not an orchestrator.

compose.yaml
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.

  1. Why is the image still large after deleting a file in a later layer?
  2. How do you get dependency caching right in CI, where the daemon starts empty?
  3. COPY versus ADD — why prefer COPY?
  4. How would you pass a secret to a build without baking it into a layer?
  5. Why does the container not shut down gracefully on docker stop?
  • CMD in shell form. The shell becomes PID 1, SIGTERM never reaches your app, and shutdown waits for the timeout.
  • :latest in 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.