Linux Labs

Containers: Docker and compose / Stage 4 of 11

Dockerfile: your own image

Not running Starting usually ~20 s
Leave the lesson

Step 3 of 7 · theory · 5 min

Layers and tags

You will see what a built image consists of and what the tag after the colon means.

Each Dockerfile instruction leaves a layer in the image — a snapshot of what it changed in the files. docker history shows the layers, the newest on top; the format keeps two columns, the size and the instruction:

text
$ sudo docker history --format 'table {{.Size}}\t{{.CreatedBy}}' site:2
SIZE      CREATED BY
0B        CMD ["httpd" "-f" "-p" "8080"]
20.5kB    RUN /bin/sh -c date -u '+%F %T' > built.txt …
12.3kB    COPY index.html . # buildkit
8.19kB    WORKDIR /www
4.58MB    BusyBox 1.38.0 (glibc), Debian 13

The bottom line is the base from FROM: the busybox image, 4.58 MB. Its layer is shared by every image based on busybox and is stored on disk once. The site layers of its own weigh kilobytes, and CMD has 0B: this instruction changes no files, it records an image setting.

A tag is the version name after the colon: site:1, site:2. Without a tag docker uses latest. A tag points to a specific image, and one image can have several tags. A build with a tag that is already taken moves it to the new image. The old one is left without a name: sudo docker ps shows a container from it with the image ID instead of the name.

Note

The machine has no network, and docker pull does not work. The preparation loaded the FROM base from a file: docker load -i /opt/images/busybox.tar. alpine.tar and nginx-alpine.tar sit next to it.

Check yourself 1 question

The web container runs from the site:2 image. You edited index.html and ran sudo docker build -t site:2 . again. What does web serve now?

  • A tag in use does not stop the build: it ends with naming to docker.io/library/site:2 done, and the old image is left without a tag.

  • Losing the tag does not stop the container: sudo docker ps shows web as Up, with the old image's ID in the IMAGE column.

  • docker needs the tag when it creates a container. The running web was created from the old image and serves its files until it is re-created.

A container stays on the image it was created from, even after the tag moves to a new one — docker ps shows the old image's ID for it. The new version reaches the site once web is re-created from the new site:2.

Key points

  • Each Dockerfile instruction is an image layer; docker history lists them from the newest down to the base.
  • The base layer from FROM is shared by every image built on that base.
  • A tag is an image's version name: a build with a taken tag moves it to the new image, while a running container stays on its own.
Ubuntu 24.04
Loading…