Docker Volume Backup & Restore Guide
In this article
This guide shows you how to safely back up and restore Docker volumes on your home server. Docker volumes store your container's data (databases, config files, uploads) separately from the container itself. Backing them up protects against accidental deletion, drive failure, or when you need to migrate to a new server. You'll learn a simple, reliable method using the `docker run` command with the `tar` utility—no extra tools required.
What You'll Need
- A home server with Docker installed (any recent version).
- Shell access (SSH or terminal) with sudo privileges.
- Enough free disk space to store the backup archive (at least the size of the volume data).
- Basic familiarity with the command line.
Step-by-Step
- Identify the volume(s) you want to back up. Run
docker volume lsto list all volumes. Note the exact name, e.g.,myapp_data. If you're unsure which volume belongs to which container, usedocker inspect <container_name>and look underMountsto see the volume names. - Stop the container(s) using the volume. This ensures data consistency. Run
docker stop <container_name>. If multiple containers share the volume, stop all of them. For databases, this is critical to avoid corruption. - Create a temporary container to archive the volume. Use the
alpineimage (it's small and includestar). Mount the volume and a host directory where the backup will be saved. For example:docker run --rm -v myapp_data:/data -v /home/user/backups:/backup alpine tar czf /backup/myapp_data_backup.tar.gz -C /data .
This creates a compressed tar file namedmyapp_data_backup.tar.gzin/home/user/backupson your host. The-C /data .ensures the archive contains the contents of the volume, not the/datafolder itself. - Verify the backup archive. Check the file exists and is not empty:
ls -lh /home/user/backups/myapp_data_backup.tar.gz. Optionally, list its contents withtar tzf /home/user/backups/myapp_data_backup.tar.gz | headto confirm the expected files are there. - Restart your containers. Run
docker start <container_name>for each stopped container. Your services are now back up and running. - To restore a volume from a backup: First, stop the container that uses the volume. Then, remove the existing volume (if you want to replace its data) with
docker volume rm myapp_data. Recreate it withdocker volume create myapp_data. - Extract the backup into the new volume. Run a temporary container to unpack the archive:
docker run --rm -v myapp_data:/data -v /home/user/backups:/backup alpine tar xzf /backup/myapp_data_backup.tar.gz -C /data
This extracts the contents of the archive into the volume. - Start the container again. Run
docker start <container_name>. Verify your application works as expected.
Common Mistakes
- Not stopping the container before backup. This can result in a corrupted or inconsistent backup, especially for databases with active writes.
- Backing up the wrong directory. Using
-C /datais essential. If you forget it, the archive will contain adatafolder, which can cause path issues when restoring. - Using a container image without
tar. Some minimal images don't include it. Stick withalpineorbusyboxto avoid this. - Overwriting a volume without a fresh copy. If you extract over an existing volume without clearing it, old files may remain and cause conflicts.
- Forgetting to test the restore. A backup is only as good as its restore. Always test on a non-production volume if possible.
Tips
- Automate backups. Put the backup command in a cron job (e.g., daily) to ensure regular snapshots.
- Use a naming convention with dates. For example,
myapp_data_2025-04-14.tar.gzmakes it easy to identify versions. - Keep multiple backups. Rotate them: keep the last 7 daily and 4 weekly backups to protect against corruption or accidental deletion.
- Consider using
--volumes-from. If you have a container with multiple volumes, you can mount them all in one backup container using--volumes-from <container_name>and archive each volume separately. - For large volumes, use uncompressed tar. Compression saves space but takes CPU time. If speed matters, omit the
zflag and use.tarinstead of.tar.gz.
Troubleshooting
- Backup command fails with "tar: /data: Cannot mkdir: Permission denied" – This usually happens when the volume is mounted with read-only permissions. Ensure the volume is writable, or run the container with
--user root(though this is rare for backups). - Restore command fails with "tar: .: Cannot mkdir: Permission denied" – The volume may not be writable by the default user in the Alpine container. Add
--user rootto the docker run command to override. - Backup file is empty or very small. Check if the volume actually contains data. Run
docker run --rm -v myapp_data:/data alpine ls -la /datato see the contents. - Container won't start after restore. This often indicates file permissions or ownership issues. Compare the original permissions using
ls -lon the volume before and after restore. You may need to adjust ownership withchowninside the container. - Volume name not found. Double-check the volume name with
docker volume ls. It's easy to mistype.
Frequently Asked Questions
Can I back up a volume while the container is running?
Technically yes, but it's risky. For databases and other write-heavy applications, you may end up with a corrupted backup. It's safer to stop the container, even for a few seconds.
What's the difference between a Docker volume and a bind mount?
A volume is managed by Docker and stored in the Docker data directory (usually /var/lib/docker/volumes/). A bind mount is a direct mapping to a host folder. The backup method here works for volumes; for bind mounts, you can simply copy the folder.
How do I back up multiple volumes at once?
You can mount multiple volumes in one temporary container and archive them separately or together. For example: docker run --rm -v vol1:/data1 -v vol2:/data2 -v /backup:/backup alpine sh -c "tar czf /backup/vol1.tar.gz -C /data1 . && tar czf /backup/vol2.tar.gz -C /data2 ."
Can I use Docker's built-in backup feature?
Docker doesn't have a built-in backup command for volumes. The method described here is the standard community approach. Some third-party tools like Portainer offer backup GUIs, but they use similar underlying mechanisms.
How do I restore a backup to a different server?
Copy the tar.gz file to the new server, create the volume with the same name, and extract the archive using the same restore command. Ensure the volume name matches what the container expects.
Should I compress the backup?
Compression saves disk space and transfer time, but it takes CPU. For most home servers, compression is recommended. If the data is already compressed (like images or videos), you can skip the z flag to save time.