Sub topic 9
How a new volume is populated from image content
This chapter assumes you can run containers with docker run, know what a named volume is, and have read the chapters on named volumes and bind mounts. It explains what happens when you mount an empty volume over a directory that already has files in the image.
The copy-up behavior
When you mount a new, empty named volume at a path that already contains files in the image, Docker copies those files into the volume first. The container then sees the image's files through the volume. This is sometimes called initialization or copy-up.
You can see it with the nginx:alpine image, which ships a default page in /usr/share/nginx/html:
docker volume create html
docker run --rm -v html:/usr/share/nginx/html alpine ls /usr/share/nginx/htmlThe second command uses the alpine image, which has no such directory, so the mount creates an empty one. Use nginx instead to see the copy:
docker run --rm -v html:/usr/share/nginx/html nginx:alpine ls /usr/share/nginx/htmlThe output lists 50x.html and index.html. They came from the image and are now stored in the html volume.
When copying happens and when it does not
Copy-up is a one-time event. It happens only if the volume is empty when the container first mounts it. After that, the volume's contents win, and later changes to the image do not reach it.
| Situation | Result |
|---|---|
| New empty named volume, image has files at the mount path | Image files are copied into the volume |
| Volume already has files | The volume's files are used. Nothing is copied |
| Bind mount | Nothing is copied. The host directory hides the image's files, even if it is empty |
| Mount path does not exist in the image | Docker creates an empty directory |
The stale volume problem
Because copy-up runs only once, a volume can hold outdated files after you upgrade an image. Suppose version 1 of an image ships /app/config/defaults.json and you mount a volume at /app/config. Version 2 changes that file. A new container using version 2 and the same volume still reads the version 1 file, because the volume was already populated.
To avoid this, keep application code out of volumes. Mount volumes only at paths that hold data the container writes, such as database files or uploads. If you must refresh a volume, remove it with docker volume rm and let the next container populate it again. That deletes any data in it, so back it up first.
Verifying the behavior yourself
This sequence shows that a populated volume is not updated when the image changes. It uses a small image built on the spot.
mkdir demo && cd demo
printf 'FROM alpine\nRUN mkdir /cfg && echo v1 > /cfg/version\n' > Dockerfile
docker build -t demo:1 .
docker run --rm -v cfgvol:/cfg demo:1 cat /cfg/version
# v1
printf 'FROM alpine\nRUN mkdir /cfg && echo v2 > /cfg/version\n' > Dockerfile
docker build -t demo:2 .
docker run --rm -v cfgvol:/cfg demo:2 cat /cfg/version
# v1 (the volume was populated by demo:1 and is not refreshed)
docker volume rm cfgvolThe second run prints v1 even though demo:2 contains v2.
Common errors
- Expecting updates from a new image. Existing volumes keep their old files. Recreate the volume or copy the new files in yourself.
- Expecting copy-up with a bind mount. Bind mounts never copy anything from the image.
- Mounting over a directory that holds the application. The volume's contents replace what the image provided once it is populated. Mount only data directories.
Exercise
You mount a new, empty named volume at /etc/app in a container. The image has /etc/app/settings.conf. Later you ship a new image with a changed settings.conf and start a container with the same volume. Which version of the file does the container see, and why?
Show answer
The old version. Docker copied the image's file into the volume the first time, when the volume was empty. The volume is no longer empty, so Docker does not copy again and the container reads the volume's existing file.
Exercise
You bind-mount an empty host directory at /usr/share/nginx/html in an nginx container. What does the container serve, and how would a named volume behave differently?
Show answer
The container serves nothing useful, because the empty host directory hides the image's default files and bind mounts never copy anything. A new named volume would be filled with the image's default files on first use.