AlgoViz

Docker

easy 26 min

Docker packages a program with its files into an image and runs it as an isolated process called a container.

See it run

Start a container step by step, reuse build layers, write into a private layer, wire networks and hit limits.

Operations

docker run

From the command to a running, isolated process.

Step 1 of 6

The command

Docker hostdocker CLIdockerdimages, networksRegistrycontainerdlife cycleruncsets up, exitsContainernot yet

Request received

docker run nginx becomes a request to the daemon.

Step 1: The command

CLI -> dockerd (REST)image missing: pull layers, check digestswritable layer, network, mountsdockerd -> containerd -> runcnamespaces, cgroups, start process 1
When
Each start
You leave with
One container

runc is gone once the container runs. If dockerd stops, live-restore can keep containers running.

In the real world

The same docker doing real jobs. Pick one and step through it.

A database in a container without a volume.

Step 1 of 3

It works

Container Aown layerContainer Bown layerImage layersread only, shared

A's layer

10,000 rows

B's layer

not used

image

database program

No volume

Rows are written into the container's own layer.

Step 1: It works

Common mistakes

The bugs interviewers watch for, with the fix beside each.

  • Copying everything before installing

    major

    Any edit invalidates the install and all later steps.

    Wrong
    COPY . .RUN npm ci
    Right
    COPY package*.json ./RUN npm ciCOPY . .
  • A secret in a layer

    critical

    Removing it in a later step leaves it in the image.

    Wrong
    COPY key.txt .RUN use key.txt && rm key.txt
    Right
    RUN --mount=type=secret,id=key \  use /run/secrets/key
  • Data in the writable layer

    critical

    It is deleted with the container.

    Wrong
    docker run postgres:18
    Right
    docker run -v pgdata:/var/lib/postgresql \  postgres:18
  • No limits and no log rotation

    major

    One container can take the host's memory or fill its disk.

    Wrong
    docker run app
    Right
    docker run -m 512m --cpus=0.5 \  --log-opt max-size=10m --log-opt max-file=3 app

Interview tips

  • How to explain it

    • A process with a private view.
    • Shared layers, one thin layer.
    • The kernel is shared.
  • Build well

    • Seldom-changed first.
    • Two stages.
    • Pin, scan, rebuild.
  • Reach for it when

    • The same build runs everywhere.
    • Services differ in dependencies.
    • Copies can be replaced.
  • Watch

    • Exit code 137.
    • CPU throttling.
    • Published ports.
    • Root and the Docker socket.