Back Up Data and Diagnose Mount Permissions
Overview
Storage failures are often permission or lifecycle failures rather than Docker bugs. Create a documented backup and restore procedure, and confirm ownership inside both the host and container before storing data.
Detailed Notes
Back up a named volume
docker run --rm -v app-data:/source:ro -v "$PWD:/backup" alpine:3.20 tar czf /backup/app-data.tgz -C /source .
Line by line:
--rmremoves the temporary container.app-data:/source:romounts the volume read-only."$PWD:/backup"mounts the current host directory for output.tar czfcreates a compressed archive.
Restore by reversing the mount:
docker run --rm -v app-data:/target -v "$PWD:/backup:ro" alpine:3.20 sh -c "tar xzf /backup/app-data.tgz -C /target"
This copies files but should not be considered a database-consistent backup for a running database. PostgreSQL, MySQL and other databases provide application-aware dumps or snapshots.
Permission failures
Common symptoms include permission denied, files owned by root, or the container creating unreadable host files.
Investigate:
docker exec app id
docker exec app ls -ld /var/lib/app
docker inspect app --format '{{json .Mounts}}'
On a bind mount, choose one strategy:
- run the application as the matching host UID/GID;
- give the shared directory a controlled group;
- copy initial data instead of granting broad host write access;
- use a volume when host edits are unnecessary.
Never solve every permission problem with chmod 777. Broad write permissions make accidental or malicious file changes easier.
Common Interview Questions
Finished this lesson?
0 of 27 lessons done · 0% of the course

