CrackOps
Interview PrepTracksLinuxAWS LabSoonInterview RoomSoon
Sign inStart free
CrackOps

Interview prep for the people who keep production up.

[email protected]
ProductInterview PrepTracksDashboardInterview Room
RolesDevOpsSREPlatformCloud
CompanyAboutContactPrivacyTerms
© CrackOps
Linux / Docker / docker volume

docker volume

Manage persistent data for containers

Intermediatestoragepersistencebackup

docker volume manages named volumes: storage that lives outside any container's writable layer, so data survives docker rm, upgrades and re-creation. Databases, uploads and anything you'd cry about losing belong in a volume (or a bind mount), never in the container.

Syntaxdocker volume ls | create | inspect | rm | prune [OPTIONS]

Watch it run

You want to prove to yourself that data written to a volume survives deleting the container.

devops@devops-lab: ~ — zsh — 96×28
➜ ~
Step 1 of 4
docker volume create appdata

An empty named volume. You could also skip this step: docker run -v appdata:/data creates it on first use.

Anatomy of a command

$
commanddocker run -d --name db

Volumes are usually created implicitly when a container mounts one by name.

How it works

A named volume with the default local driver is a directory under /var/lib/docker/volumes/<name>/_data. When a container starts, dockerd bind-mounts that directory into the container's mount namespace at the target path, so reads and writes bypass the overlay filesystem entirely: no copy-on-write, full disk speed. The volume is a separate, reference-counted object: removing a container never removes a named volume, and docker volume rm refuses while any container (even a stopped one) uses it. The first time an empty named volume is mounted over a path that has files in the image, Docker copies those files in; bind mounts never do. Other drivers (NFS options on local, or plugins) put the data on network storage instead of the host disk.

Options you'll actually use

lsList volumes (-f dangling=true for unused ones)
create NAMECreate a volume explicitly (-d driver, -o driver options, --label)
inspect NAMEDriver, mountpoint on the host, labels, creation time
rm NAMEDelete a volume and all its data (refused while in use)
pruneDelete unused anonymous volumes; -a also deletes unused named ones
-v NAME:/path[:ro]Mount a named volume (on docker run)
-v /host/path:/pathBind-mount a host directory instead
--tmpfs /pathIn-memory mount, gone when the container stops

Common mistakes

Running a database without -v and upgrading with docker rm + docker run

The data was in the container's writable layer (or an anonymous volume you then lose track of) and is gone with the container.

docker run -v pgdata:/var/lib/postgresql/data postgres:16 from day one
docker volume prune -a (or system prune -a --volumes) on a server

Removes every volume no container is using right now, including databases whose container is briefly removed during an upgrade.

List with docker volume ls -f dangling=true, then remove by name
Copying /var/lib/docker/volumes/<db>/_data while the database runs

A file-level copy of a live database is usually inconsistent and may not restore.

pg_dump/mysqldump, or stop the container before the file backup
Bind-mounting a host folder and getting 'permission denied' inside

The container runs as a uid (e.g. 999 for postgres) that doesn't own the host directory.

chown the host dir to the container's uid, or use a named volume

Interview questions

Practice

Try these on a Linux machine of your own. Each task has a hint and a solution.

  1. 1

    Back up the appdata volume (create it and write a file into it first) to ~/backups/appdata-<today>.tgz with a throwaway alpine container, then list what's inside.

  2. 2

    Write the docker command that restores ~/backups/pgdata.tgz into a new empty volume pgdata2.

  3. 3

    Write the command that lists only volumes not used by any container.

Related commands

docker runCreate and start a container from an imagedocker rmRemove containersdocker system pruneReclaim disk space from unused Docker datapg_dumpBack up a PostgreSQL database consistentlytarBundle files into an archive and extract them
Previousdocker networkNextdocker system prune
Linux / Docker / docker volume

docker volume

Manage persistent data for containers

Intermediatestoragepersistencebackup

docker volume manages named volumes: storage that lives outside any container's writable layer, so data survives docker rm, upgrades and re-creation. Databases, uploads and anything you'd cry about losing belong in a volume (or a bind mount), never in the container.

Syntaxdocker volume ls | create | inspect | rm | prune [OPTIONS]

Watch it run

You want to prove to yourself that data written to a volume survives deleting the container.

devops@devops-lab: ~ — zsh — 96×28
➜ ~
Step 1 of 4
docker volume create appdata

An empty named volume. You could also skip this step: docker run -v appdata:/data creates it on first use.

Anatomy of a command

$
commanddocker run -d --name db

Volumes are usually created implicitly when a container mounts one by name.

How it works

A named volume with the default local driver is a directory under /var/lib/docker/volumes/<name>/_data. When a container starts, dockerd bind-mounts that directory into the container's mount namespace at the target path, so reads and writes bypass the overlay filesystem entirely: no copy-on-write, full disk speed. The volume is a separate, reference-counted object: removing a container never removes a named volume, and docker volume rm refuses while any container (even a stopped one) uses it. The first time an empty named volume is mounted over a path that has files in the image, Docker copies those files in; bind mounts never do. Other drivers (NFS options on local, or plugins) put the data on network storage instead of the host disk.

Options you'll actually use

lsList volumes (-f dangling=true for unused ones)
create NAMECreate a volume explicitly (-d driver, -o driver options, --label)
inspect NAMEDriver, mountpoint on the host, labels, creation time
rm NAMEDelete a volume and all its data (refused while in use)
pruneDelete unused anonymous volumes; -a also deletes unused named ones
-v NAME:/path[:ro]Mount a named volume (on docker run)
-v /host/path:/pathBind-mount a host directory instead
--tmpfs /pathIn-memory mount, gone when the container stops

Common mistakes

Running a database without -v and upgrading with docker rm + docker run

The data was in the container's writable layer (or an anonymous volume you then lose track of) and is gone with the container.

docker run -v pgdata:/var/lib/postgresql/data postgres:16 from day one
docker volume prune -a (or system prune -a --volumes) on a server

Removes every volume no container is using right now, including databases whose container is briefly removed during an upgrade.

List with docker volume ls -f dangling=true, then remove by name
Copying /var/lib/docker/volumes/<db>/_data while the database runs

A file-level copy of a live database is usually inconsistent and may not restore.

pg_dump/mysqldump, or stop the container before the file backup
Bind-mounting a host folder and getting 'permission denied' inside

The container runs as a uid (e.g. 999 for postgres) that doesn't own the host directory.

chown the host dir to the container's uid, or use a named volume

Interview questions

Practice

Try these on a Linux machine of your own. Each task has a hint and a solution.

  1. 1

    Back up the appdata volume (create it and write a file into it first) to ~/backups/appdata-<today>.tgz with a throwaway alpine container, then list what's inside.

  2. 2

    Write the docker command that restores ~/backups/pgdata.tgz into a new empty volume pgdata2.

  3. 3

    Write the command that lists only volumes not used by any container.

Related commands

docker runCreate and start a container from an imagedocker rmRemove containersdocker system pruneReclaim disk space from unused Docker datapg_dumpBack up a PostgreSQL database consistentlytarBundle files into an archive and extract them
Previousdocker networkNextdocker system prune