Fatskills
Practice. Master. Repeat.
Study Guide: Docker Docker Data Persistence Backup and Restore of Volumes
Source: https://www.fatskills.com/kubernetes/chapter/docker-docker-data-persistence-backup-and-restore-of-volumes

Docker Docker Data Persistence Backup and Restore of Volumes

By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.

⏱️ ~3 min read

1. Core Command / Concept

Backup and restore of Docker volumes allows you to save and recover data in case of container failure or data loss. docker run -v.

2. Step-by-Step

  1. Create a new directory to store your Docker volumes: mkdir my-volumes
  2. Create a new Docker container with a volume: docker run -d -v my-volumes:/data --name my-container busybox
  3. Check the volume is created: docker inspect my-container | grep "Mounts"
  4. Create a new file in the container: docker exec -it my-container touch /data/test.txt
  5. ⚠️ Stop the container: docker stop my-container
  6. Backup the volume: docker run -v my-volumes:/backup --rm -it alpine tar -cvf - /backup > backup.tar
  7. Clean up: docker rm -f my-container
  8. Restore the volume: docker run -v my-volumes:/data --rm -it alpine tar -xvf backup.tar -C /data

3. Real Commands with Examples


Example 1: Backup a volume

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.

Example 2: Restore a volume

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.

Example 3: Backup and restore a volume with a different name

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.

4. Common Errors


Error 1: Volume not found

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.

Error 2: Permission denied

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 ....

Error 3: Tar file not found

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.

5. Quick Checks


Scenario 1: Volume exists

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", ...}.

Scenario 2: Container has a volume

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", ...}].

Scenario 3: Tar file exists

Situation: You want to check if a tar file exists.
Command: ls backup.tar Correct response: backup.tar.

6. Last-Minute Reference

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.




ADVERTISEMENT