Lesson 05 beginner

Key Docker Concepts

The vocabulary you need for everything else: images, layers, containers, registries, tags and digests, Dockerfiles, volumes, networks and Compose.

Docker has a small vocabulary, and once it clicks, every command makes sense. This lesson defines each term, shows how they connect, and gives you one command per idea so you can see it on your machine.

The building blocks at a glance

ConceptWhat it isYou meet it in
ImageRead-only template: files plus metadata such as the start commanddocker pull, docker build
LayerOne read-only filesystem diff inside an imagedocker history
ContainerA running or stopped instance of an image, with its own writable layerdocker run, docker ps
DockerfileThe recipe that builds an image, one instruction per stepdocker build
RegistryA server that stores and distributes imagesdocker push, docker pull
Repository and tagAn image name and a version label, like nginx:1.27every image reference
VolumeStorage managed by Docker that outlives containersdocker volume, -v
NetworkA virtual network that lets containers reach each other by namedocker network
ComposeA YAML file describing a multi-container applicationdocker compose up

Images are made of layers

Each instruction in a Dockerfile that changes files produces a layer. Layers are stacked, cached and shared: ten images based on node:22-alpine store that base only once on disk.

$ docker history --format 'table {{.CreatedBy}}\t{{.Size}}' hello-node:1.0
CREATED BY                                      SIZE
CMD ["node" "server.js"]                        0B
EXPOSE map[3000/tcp:{}]                         0B
ENV PORT=3000                                   0B
COPY server.js . # buildkit                     8.19kB
WORKDIR /app                                    0B
...                                             152MB

Instructions like ENV and CMD only change metadata, so their layers are empty. Lesson 8 shows how ordering layers well makes builds much faster.

Containers add a writable layer

When a container starts, Docker puts a thin writable layer on top of the image’s read-only layers. Anything the process writes goes there:

$ docker run --name scratchpad alpine sh -c 'echo hi > /note.txt'
$ docker diff scratchpad
A /note.txt
$ docker rm scratchpad
scratchpad

A means added. Removing the container throws the writable layer away, which is why important data belongs in a volume (lesson 9).

Names, registries, tags and digests

A full image reference has up to four parts:

ghcr.io/jeff/api:1.4.2
  • ghcr.io is the registry. When omitted, Docker uses Docker Hub (docker.io).
  • jeff/api is the repository. Official Docker Hub images like nginx live under library/.
  • 1.4.2 is the tag. When omitted, Docker uses latest, which is just a label, not “the newest”.
  • A digest such as @sha256:3b1f... pins the exact content and never changes.
$ docker pull redis:7.4
$ docker images redis
REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
redis        7.4       8e1f3c5a9d2b   2 weeks ago   117MB

The container lifecycle

A container moves through a few states, and every state has a command:

  1. Created: docker create (or the first half of docker run).
  2. Running: docker start, or docker run directly.
  3. Paused: docker pause freezes the processes; docker unpause resumes.
  4. Exited: the main process ended, or you ran docker stop.
  5. Removed: docker rm deletes the container and its writable layer.
$ docker ps -a --format 'table {{.Names}}\t{{.Status}}'
NAMES     STATUS
api       Up 3 hours
worker    Exited (1) 10 minutes ago

Networks connect containers

Containers on the same user-defined network can reach each other by name, through Docker’s built-in DNS:

$ docker network create appnet
$ docker run -d --name cache --network appnet redis:7.4
$ docker run --rm --network appnet redis:7.4 redis-cli -h cache ping
PONG

No IP addresses and no published ports were needed; -p is only for reaching a container from outside Docker.

Compose ties it together

Compose describes images, containers, networks and volumes for a whole application in a compose.yaml file. You saw one in lesson 2; it uses every concept on this page, and docker compose up creates them in the right order.

Mental model: a Dockerfile builds one image; a Compose file runs many containers from images and wires them together.

Exercise

Containers web and db are attached to the same user-defined network. How should web connect to the database?

  1. Through the host’s IP address and a port published with -p
  2. By hardcoding the container IP shown by docker inspect
  3. It cannot: containers must be linked with --link
  4. By using the container name db as the hostname
Show answer

Answer: D. User-defined networks have built-in DNS that resolves container names. No port needs to be published for container-to-container traffic, and container IPs change on every restart.

Key takeaways

  • Images are stacks of cached, shareable layers; containers add one writable layer on top.
  • A reference like registry/repository:tag locates an image; a digest pins its exact content.
  • Data written inside a container disappears with it unless it lives in a volume.
  • User-defined networks let containers talk to each other by name.

FAQ

What is the difference between an image ID and a digest?

The image ID identifies the image configuration on your machine. The digest identifies the image manifest in a registry. Use digests when you need a reference that is identical everywhere.

Why are there so many dangling images named none?

They are old layers left behind when a tag moves to a new build. docker image prune removes them safely.

What does OCI mean?

The Open Container Initiative defines open standards for image formats and runtimes. Docker, Podman, containerd and Kubernetes all speak OCI, so an image built with one runs with the others.

↑ ↓ navigate Enter open Esc close