Layers
An image is a stack of layers, one per instruction
Each instruction adds a layer. Stacked, they are the container's file system.
An image is the package a container starts from, and a Dockerfile is its recipe. Read the recipe one line at a time and watch what each line adds. The first line borrows someone else's image as a base. That is most of the weight. Setting the working directory adds no files. The list of packages is one tiny file. Installing them adds forty eight megabytes. Then your own code, which is small. And last, the command to start, which is only a setting.
Layers · Going deeper · 1 of 2
Lab: build twice and watch the cache
Build, read the history, change one file, build again.
ENTRYPOINT and CMD · Going deeper · 2 of 2
ENTRYPOINT is the program; CMD is its default arguments
Kubernetes command replaces ENTRYPOINT. args replaces CMD.
The build cache
When one layer changes, all the later ones rebuild
Put what rarely changes first and your code last.
Layers make builds fast, if you order them well. Suppose you fix one line of code and build again. In this version, all files were copied before the packages were installed. The copy step sees a changed file, so it runs again, and so does everything after it, including the slow install. In this version only the package list is copied first. It has not changed, so the install is reused, and just the final copy runs.
Tags and digests
A tag can move; a digest never does
A tag is a movable label. A digest is the content's fingerprint.
An image's full name has three parts: the registry that stores it, the repository, and a tag or a digest that picks one version. The difference between those last two matters. On Monday, the tag one point four names this image. On Friday the publisher fixes a bug and pushes again under the same tag. The label has moved, and anyone who pulls now gets different contents. A digest cannot do that, because it is calculated from the contents.
Names and pull policy · Going deeper
What a short name stands for, and when a node pulls again
latest means Always. Any other tag means IfNotPresent.
Small images
The base image decides most of the size
Same Python, three bases: 1,020, 125 and 50 megabytes.
Since the base is most of the image, choosing it is the biggest size decision you make. The default Python image is built on a full Debian system with compilers. It is over a gigabyte. The slim variant removes what Python does not need at run time. The Alpine variant uses a tiny Linux distribution, and is smaller still. Smaller images travel faster, and they contain fewer packages that might need a security patch.
Small images · Going deeper
Multi-stage builds: compile in one image, ship another
Build in a workshop stage. Ship only the finished program.
Registries
Only the layers you lack are downloaded
Layers are shared. A pull moves only what is missing.
Images are kept in a registry, and the layers pay off again when images move. Here is version one point four in a registry, as three layers. A server that ran the previous version already holds the base and the packages. Their digests match, so there is nothing to fetch. Only the layer with your new code travels: two megabytes, not one hundred and seventy five.
Inside a registry · Going deeper
A tag names an index; the index names one manifest for each platform
Index, manifest, config, layers: each named by its digest.
Lesson complete
You finished “Building and sharing images”
Nicely done.