CodeBlanks Logo Icon CodeBlanks

Volume management

Learn to manage volumes with docker volume ls and rm.

Volume management

The docker volume subcommands:

docker volume ls                 # list
docker volume inspect pgdata     # details: Mountpoint, creation
docker volume rm pgdata          # delete (no container must use it)
docker volume prune              # delete anonymous volumes no container uses
docker volume prune -a           # also the unused named ones

Deleting a volume can't be undone; take a backup first for database volumes.

Backing up a volume

The standard way to back up a volume is to mount it and a host folder into a temporary container and create a tar archive:

docker run --rm -v pgdata:/veri:ro -v "$PWD":/yedek alpine \
  tar czf /yedek/pgdata.tar.gz -C /veri .

# restoring
docker run --rm -v pgdata:/veri -v "$PWD":/yedek alpine \
  tar xzf /yedek/pgdata.tar.gz -C /veri

Copying a running database's files can be inconsistent; the database should also be backed up with its own tools (pg_dump).

Practice A volume backup

Write two commands: list the volumes; then in an alpine container deleted when done, mount pgdata read-only at /veri and the current folder ("$PWD") at /yedek, and run tar czf /yedek/pgdata.tar.gz -C /veri .

Starter code

terminal
# buraya yaz

Sample solution

terminal
docker volume ls
docker run --rm -v pgdata:/veri:ro -v "$PWD":/yedek alpine tar czf /yedek/pgdata.tar.gz -C /veri .

Expected output

DRIVER    VOLUME NAME
local     pgdata
local     redisdata

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 After docker rm -f db, what happens to the pgdata volume db had mounted?

  • It is added to the image
  • It gets corrupted
  • It stays; it must be deleted separately Correct answer
  • It is deleted automatically

Explanation: docker rm -v only deletes anonymous volumes along with it.

2 Show the volume's details.

Question
docker volume ____ pgdata
Answer
docker volume inspect pgdata

Explanation: inspect gives information like the Mountpoint as JSON.

3 Match.

Term Meaning
volume ls List
volume inspect Details
volume rm Delete
tar czf Backup archive

All lessons in Beginner