Skip to content

Chapter 6 of 8

Storage and Volumes

Containers are ephemeral by default, but Docker provides several mechanisms for persisting data outside the container's writable layer. A Docker volume is a persistent storage object managed by Docker itself and stored in /var/lib/docker/volumes/. There are three flavors: named volumes (created explicitly with docker volume create mydata and easy to back up, list, and reuse), bind mounts (which map an arbitrary host path directly into a container and are favored in development for live-reload workflows), and tmpfs mounts (which live in the host's RAM and swap, making them extremely fast but non-persistent, and ideal for secrets or temporary caches). The VOLUME instruction in a Dockerfile declares a mount point for externally attached storage, and docker run --tmpfs /tmp:size=100m attaches a memory-backed mount at /tmp.

Anonymous volumes are created implicitly when a Dockerfile declares VOLUME without a name or when you run docker run -v /data. Docker generates a random name, which makes them hard to reference later, so the recommended pattern is to use named volumes in production. When a container is removed, its volumes are not deleted by default, which protects data, but you can use docker rm -v to also clean up anonymous volumes associated with a container, or docker volume prune to remove all unused volumes. Volume drivers and plugins extend Docker to mount external storage such as NFS, AWS EBS, Azure Disk, GCE Persistent Disk, and Ceph; the built-in local driver handles simple cases and others are installed separately. Bind mounts also have a propagation mode (rprivate, private, rshared, shared, slave, rslave) that controls how mounts created inside a mount propagate to the host, though most users should leave this at its default.

All chapters
  1. 1Docker Fundamentals
  2. 2Writing Dockerfiles
  3. 3Building and Distributing Images
  4. 4The Docker CLI
  5. 5Networking
  6. 6Storage and Volumes
  7. 7Docker Compose
  8. 8Security and Resource Management

Drill it

Reading is not remembering. These come from the Docker Containers deck:

Q

What is Docker?

Docker is an open-source platform that automates the deployment of applications inside lightweight, portable containers. Containers package an application with...

Q

What is a Docker container?

A Docker container is a runnable instance of a Docker image. It is an isolated process that shares the host OS kernel but has its own filesystem, networking, an...

Q

What is a Docker image?

A Docker image is a read-only template used to create containers. It consists of layered filesystems built from a Dockerfile. Images are stored in registries an...

Q

What does the FROM instruction do in a Dockerfile?

FROM sets the base image for subsequent instructions. It must be the first instruction in a Dockerfile (aside from ARG).Example: FROM python:3.11-slimYou can al...