Sub topic 8
Anonymous volumes and the VOLUME instruction
This chapter assumes you can run containers with docker run, you know what a named volume is, and you have read the chapter on managing volumes with docker volume ls and docker volume rm.
What an anonymous volume is
An anonymous volume is a volume Docker creates without a name you chose. Docker gives it a random 64-character hexadecimal ID instead. It works like a named volume: the data lives outside the container's writable layer and survives a docker stop. The difference is that nothing you type later can easily refer to it.
You create one by giving -v only a container path:
docker run -d --name anon -v /data alpine sleep 3600
docker volume lsThe docker volume ls output lists a volume whose name is a long random string. Compare that with -v mydata:/data, which creates a volume called mydata.
The VOLUME instruction
A Dockerfile can declare that a path holds data that should live outside the writable layer. The VOLUME instruction does this:
FROM alpine:3.20
RUN mkdir -p /var/lib/app && echo "seed" > /var/lib/app/state.txt
VOLUME /var/lib/app
CMD ["cat", "/var/lib/app/state.txt"]Every container started from this image gets an anonymous volume mounted at /var/lib/app unless you supply your own mount for that path. Many official images do this. The postgres image declares VOLUME /var/lib/postgresql/data, so a database container started without -v still writes its files to a volume.
Why anonymous volumes pile up
Removing a container does not remove its anonymous volume by default. Run the same image a few times and you collect orphaned volumes:
docker build -t volume-demo .
docker run --name run1 volume-demo
docker rm run1
docker volume lsThe volume from run1 is still listed. Because it has no meaningful name, you cannot tell which container or image it belonged to. Two ways to clean up:
- Pass
-vtodocker rm, or--rmtodocker run, so Docker deletes the container's anonymous volumes with it. - Run
docker volume pruneto remove all volumes that no container references.
docker rm -v run2
docker run --rm volume-demoNeither option touches named volumes. Those stay until you remove them explicitly.
Finding and keeping an anonymous volume
If a container you need wrote to an anonymous volume, find it with docker inspect:
docker inspect --format '{{ json .Mounts }}' anonThe Name field in the output is the volume ID. You can then mount it by that name in a new container with -v <id>:/data. This works, but it is awkward. If you care about the data, use a named volume from the start. Mounting a named volume at the same path overrides the VOLUME default:
docker run -d --name db -e POSTGRES_PASSWORD=example \
-v pgdata:/var/lib/postgresql/data postgres:16Common errors
- Lost data after "recreating" a container. You removed the container and started a new one. The new container got a fresh anonymous volume, and the old data sits in an unreferenced one. Find it with
docker volume lsanddocker inspect. - Disk filling up with unnamed volumes. Containers started repeatedly from images with
VOLUMEleave volumes behind. Use--rmordocker rm -v. - Build steps that change a volume path being ignored. In a Dockerfile, changes made to a path after the
VOLUMEinstruction can be discarded in the final image. Put theRUNsteps that write to that path beforeVOLUME, as in the example above.
Exercise
You run docker run --name web -d image-with-volume, where the image declares VOLUME /data. Later you run docker rm -f web and start a new container from the same image. Is the data from the first container available in the second? Why, and what should you do if you want it to be?
Show answer
No. The first container's data is in an anonymous volume that still exists but is not attached to the new container, which gets a fresh anonymous volume. To keep data across containers, mount a named volume at the path, for example -v appdata:/data, every time you start the container.
Exercise
Which two options delete a container's anonymous volumes when the container is removed?
Show answer
Use docker rm -v <container> when removing the container, or start it with docker run --rm. Neither affects named volumes.