By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.
Backup and restore of Docker volumes allows you to save and recover data in case of container failure or data loss. docker run -v.
mkdir my-volumes
docker run -d -v my-volumes:/data --name my-container busybox
docker inspect my-container | grep "Mounts"
docker exec -it my-container touch /data/test.txt
docker stop my-container
docker run -v my-volumes:/backup --rm -it alpine tar -cvf - /backup > backup.tar
docker rm -f my-container
docker run -v my-volumes:/data --rm -it alpine tar -xvf backup.tar -C /data
Purpose: Backup a Docker volume to a tar file.Command: docker run -v my-volumes:/backup --rm -it alpine tar -cvf - /backup > backup.tar Flags: -v mounts the volume, --rm removes the container after it stops, -it allocates a pseudo-TTY, tar is the command to create a tar archive, -cvf - creates a verbose tar file to stdout.
-v
--rm
-it
tar
-cvf -
Purpose: Restore a Docker volume from a tar file.Command: docker run -v my-volumes:/data --rm -it alpine tar -xvf backup.tar -C /data Flags: -v mounts the volume, --rm removes the container after it stops, -it allocates a pseudo-TTY, tar is the command to extract a tar archive, -xvf extracts the tar file, -C changes to the specified directory.
-xvf
-C
Purpose: Backup and restore a Docker volume with a different name.Command: docker run -v my-volumes:/backup --rm -it alpine tar -cvf - /backup > backup.tar (backup) and docker run -v my-new-volumes:/data --rm -it alpine tar -xvf backup.tar -C /data (restore) Flags: Same as above.
docker run -v my-new-volumes:/data --rm -it alpine tar -xvf backup.tar -C /data
Error message: docker: Error response from daemon: Volume not found: my-volumes.Why it happens: The volume does not exist.One-line fix: docker volume create my-volumes.
docker: Error response from daemon: Volume not found: my-volumes
docker volume create my-volumes
Error message: docker: Error response from daemon: Permission denied.Why it happens: The user does not have permission to access the volume.One-line fix: sudo docker run -v my-volumes:/backup ....
docker: Error response from daemon: Permission denied
sudo docker run -v my-volumes:/backup ...
Error message: docker: Error response from daemon: No such file or directory.Why it happens: The tar file does not exist.One-line fix: docker run -v my-volumes:/backup --rm -it alpine tar -cvf - /backup > backup.tar.
docker: Error response from daemon: No such file or directory
Situation: You want to check if a volume exists.Command: docker volume inspect my-volumes Correct response: {"Mountpoint": "/var/lib/docker/volumes/my-volumes/_data", ...}.
docker volume inspect my-volumes
{"Mountpoint": "/var/lib/docker/volumes/my-volumes/_data", ...}
Situation: You want to check if a container has a volume.Command: docker inspect my-container | grep "Mounts" Correct response: "Mounts": [{"Type": "volume", "Name": "my-volumes", ...}].
"Mounts": [{"Type": "volume", "Name": "my-volumes", ...}]
Situation: You want to check if a tar file exists.Command: ls backup.tar Correct response: backup.tar.
ls backup.tar
backup.tar
• docker run -v: mounts a volume.• --rm: removes the container after it stops.• -it: allocates a pseudo-TTY.• tar -cvf -: creates a verbose tar file to stdout.• tar -xvf: extracts a tar file.• -C: changes to the specified directory.• docker volume create: creates a new volume.• docker volume inspect: inspects a volume.• docker inspect: inspects a container.• ⚠️ docker rm -f: deletes all stopped containers.
Join 4M+ learners. Unlock unlimited quizzes, wrong-answer tracking, flashcards + reminders, study guides, and 1-on-1 challenges.