Docker · Containers in practice

What are Docker volumes and when should you use them?

EasyvolumespersistencestorageUpdated Oct 2026
Quiz this track

Short answer

Volumes are storage managed by Docker and stored outside the container filesystem. They survive container removal and are the default choice for persistent application data.

A volume is a directory on the host, usually under /var/lib/docker/volumes, mounted into a container. It is independent of the container lifecycle, so deleting and recreating a container keeps the data. Volumes are also easier to back up and share between containers than the writable layer.

sh
docker volume create pgdata
docker run -d --name db -v pgdata:/var/lib/postgresql/data postgres:16
docker volume ls
docker volume inspect pgdata
A named volume keeps database files when the container is replaced.

Volumes versus other options

  • Named volume: Docker-managed, good for databases and durable state
  • Anonymous volume: created automatically, easy to lose track of
  • Bind mount: a host path, useful for development and host integration
  • tmpfs: memory-backed, suitable for scratch data that need not survive

Volumes do not replace backups. They live on one host, can be deleted with docker volume prune, and are not replicated. A production database needs a real backup and restore process, and often should run outside the container platform entirely.

Mounting an empty named volume over a directory that already contains files copies the image contents in on first use. This surprises people when they expect the directory to be empty.

Follow-ups you should expect

Interviewers use these to check whether the first answer was understanding or memorisation.

  1. 1How is a named volume different from an anonymous volume?
  2. 2What happens to a volume when its container is removed?
  3. 3Why is a volume not a backup?
  4. 4When would you use tmpfs instead?

Say it in the interview

Volumes are Docker-managed storage that lives outside the container filesystem, so data survives container replacement. I use a named volume for state such as a database directory, and I mount it explicitly so it is not left as an anonymous volume. A volume is still host-local and deletable, so I pair it with a tested backup and restore process.

Use this as a model, not a script. Saying it in your own words is what makes it survive follow-up pressure.

Related questions

Reading is step one

Now say it out loud.

Understanding an answer and delivering it under pressure are different skills. Rehearse this topic in a live Docker mock interview and get scored feedback on depth, clarity and confidence.