This flow shows the steps to backup Docker data and restore it when needed, ensuring data safety.
Execution Sample
Docker
docker run --rm --volumes-from mycontainer -v $(pwd):/backup busybox tar cvf /backup/backup.tar /data
docker run --rm --volumes-from mycontainer -v $(pwd):/backup busybox tar xvf /backup/backup.tar -C /data
This code backs up a Docker container's data volume to a tar file and restores it from that file.
Process Table
Step
Action
Command
Result
1
Identify data volume to backup
mycontainer volume /data
Volume /data identified
2
Run backup container to create tar
docker run --rm --volumes-from mycontainer -v $(pwd):/backup busybox tar cvf /backup/backup.tar /data
backup.tar created in current directory
3
Store backup file safely
backup.tar saved locally
Backup file ready for restore
4
Run restore container to extract tar
docker run --rm --volumes-from mycontainer -v $(pwd):/backup busybox tar xvf /backup/backup.tar -C /
Data restored into /data volume
5
Verify restored data
Check container data
Data matches backup
6
Exit
No further commands
Backup and restore complete
💡 Backup and restore process ends after data verification
Status Tracker
Variable
Start
After Backup
After Restore
Final
backup.tar
Not present
Created with container data
Used to restore data
Exists as backup file
/data volume
Has original data
Data unchanged
Replaced with restored data
Data restored successfully
Key Moments - 3 Insights
Why do we use a separate container with --volumes-from to backup data?
Because the data lives inside the container volume, the backup container accesses it via --volumes-from to copy data without stopping the original container (see execution_table step 2).
What happens if we restore data to a wrong volume path?
The restore command extracts files to the specified path. If wrong, data won't be restored correctly, causing mismatch during verification (see execution_table step 4 and 5).
Why do we mount the current directory to /backup in the container?
Mounting $(pwd) to /backup lets the container write the backup.tar file to the host machine, making it accessible outside the container (see execution_table steps 2 and 4).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, at which step is the backup.tar file created?
AStep 1
BStep 2
CStep 4
DStep 5
💡 Hint
Check the 'Result' column for the step where backup.tar is created.
According to the variable tracker, what is the state of /data volume after restore?
AReplaced with restored data
BHas original data
CData unchanged
DData lost
💡 Hint
Look at the 'After Restore' column for /data volume in variable_tracker.
If we skip mounting $(pwd) to /backup, what happens to the backup file?
ABackup fails with error
BIt is saved on the host machine
CIt is saved inside the container and lost after it exits
DBackup file is saved in /data volume
💡 Hint
Refer to key_moments about mounting current directory for backup file accessibility.
Concept Snapshot
Backup and restore Docker volumes:
- Use 'docker run --volumes-from' to access container volumes
- Mount host dir to container to save backup files
- Use tar to archive and extract volume data
- Verify data after restore
- Keep backups safe and test restores regularly
Full Transcript
This lesson shows how to backup and restore Docker container data volumes. First, identify the volume to backup. Then run a temporary container with --volumes-from to access the volume and mount the current directory to save the backup tar file. Use tar commands inside the container to create and extract backups. After restoring, verify the data matches the original. This method keeps data safe and allows recovery if needed.