Images and containers
We already know that an image carries the app's files and a container is a process started from an image. But a process writes files: logs, temporary data, settings. Where do these writes go, if the image must stay the same for everyone?
Back to the port. The cargo in a shipping container is sealed: nobody changes it on the way, and the receiver sees exactly what the sender sent. An image is the same sealed cargo. To work with it, Docker puts an empty tray on top of the cargo, and the process puts everything new only there. The seal stays intact.
An image is read-only layers
An image is made of layers. A layer is the set of files that one build step added or changed: the distribution's files, then the installed libraries, then the app's code. Docker stacks the layers on top of each other, and the process sees them as one file system.
An image's layers are read-only. Starting a container never changes the image, so the same image is identical on a laptop, on a server and in the thousandth container.
A container is an image plus a layer for writes
A container is the image's layers plus one thin writable layer on top, its own for each container. Everything the process creates, changes or deletes lands only in this layer. Remove the container, and its layer is removed, and all the changes are gone with it. The image's layers stay untouched.
Both containers read the same image layers. Each writes only to its own top layer.
Let's check. We start a container that creates a file and look at what changed in it:
docker run --name note-a alpine:3.21 sh -c 'echo hello > /note.txt'
docker diff note-a
docker run --rm alpine:3.21 cat /note.txtA /note.txt
cat: can't open '/note.txt': No such file or directoryLet's go step by step:
- The first command starts a container named
note-a. It writeshelloto the file/note.txtand exits. Without the--rmflag, the stopped container stays on disk together with its layer. docker diff note-ashows how the container's writable layer differs from the image. The letterAmeans "added".- The third command starts a new container from the same image. The file is not there: the new container has its own empty writable layer, and the image did not change.
After docker rm note-a the container is removed together with its layer, and the file /note.txt is gone for good.
Here the tray picture stops working. The process does not see the cargo and the tray separately, but one combined file system. If it changes a file from the image, Docker copies that file into the writable layer and changes the copy. If it deletes one, Docker leaves a "no such file" mark in the layer, and the file itself stays in the image.
One image, many containers
Each container has its own writable layer, and the image's layers are shared. Let's start two containers from the image nginx:1.27.3 and ask for their sizes:
docker run -d --name web-1 nginx:1.27.3
docker run -d --name web-2 nginx:1.27.3
docker ps -s --filter name=web- --format 'table {{.Names}}\t{{.Size}}'d38f50fe0e94dba73c159c0fe959fbd7b5d57fa206ea967ff72ebb24a0a54876
f27ddc9cc34d975428ca25d6421854ce7fbfe5fd8f4a234bc4539f2197971823
NAMES SIZE
web-2 1.09kB (virtual 192MB)
web-1 1.09kB (virtual 192MB)What we see here:
- The
-dflag starts a container in the background, and Docker prints its full id. docker ps -slists running containers with their sizes, and--formatkeeps only the name and the size.1.09kBis the container's writable layer: nginx wrote a few service files at startup.virtual 192MBis the image's layers together with the container's layer. These 192 MB are on disk once, and both containers read the same layers.
So the tenth container from the same image starts in a fraction of a second and takes almost no space: the image does not need to be copied.
What outlives a container
The writable layer lives as long as the container:
- stopping and restarting a container keeps the layer;
docker rmremoves the container together with its layer;- a new container from the same image starts with an empty layer.
Data that must outlive the container, such as database files, is kept outside the writable layer. Docker has separate storage mechanisms for that.
Common mistake
Fixing the app inside a running container. Say a container has a wrong config file. You get into the container, edit the file, and everything works. At the next release the container is removed and created again from the image, and the fix is gone: it lived only in the writable layer. The fix goes where the image is built from, and a new image is built.
Key points
- An image is read-only layers; starting a container does not change the image.
- A container is the image's layers plus its own thin writable layer on top.
- Remove a container, and all its changes are gone; a new container starts with an empty layer.
- Image layers are shared: ten containers from one image do not copy it ten times.