Volume management
- Docker & Kubernetes course
- Beginner
- Lesson 53/82
- 4 min
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 onesDeleting 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 /veriCopying 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
# buraya yazSample solution
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.
docker volume ____ pgdata
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 |