0
0
Dockerdevops~5 mins

Backup and restore strategies in Docker - Commands & Configuration

Choose your learning style9 modes available
Introduction
Backing up data means saving a copy of important information so you don't lose it. Restoring means putting that saved data back if something goes wrong. In Docker, this helps protect your app's data stored in containers or volumes.
When you want to save your database data before updating your app container
When you need to move your app data from one server to another
When you want to protect your files stored in Docker volumes from accidental deletion
When you want to create a snapshot of your app's state before testing new features
When you want to recover your app data after a container crash or failure
Commands
This command creates a backup of the /data directory inside the 'my-app-container' container. It uses a temporary busybox container to access the volume and saves the backup.tar file to your current folder.
Terminal
docker run --rm --volumes-from my-app-container -v $(pwd):/backup busybox tar cvf /backup/backup.tar /data
Expected OutputExpected
data/ data/file1.txt data/file2.txt
--volumes-from - Mounts volumes from the specified container
-v - Binds a host directory to the container for saving the backup
--rm - Removes the temporary container after the command finishes
This command restores the backup.tar file from your current folder into the Docker volume named 'my-app-data'. It uses a temporary busybox container to extract the files into the volume.
Terminal
docker run --rm -v $(pwd):/backup -v my-app-data:/data busybox tar xvf /backup/backup.tar -C /data
Expected OutputExpected
data/ data/file1.txt data/file2.txt
-v - Binds host folder and Docker volume to the container
--rm - Removes the temporary container after restoring
Lists all Docker volumes on your system to verify that the volume 'my-app-data' exists before restoring data.
Terminal
docker volume ls
Expected OutputExpected
DRIVER VOLUME NAME local my-app-data
Shows all containers to check if your app container is running or stopped before backup or restore.
Terminal
docker ps -a
Expected OutputExpected
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES abc123def456 my-app-image "/bin/sh -c 'npm start'" 10 minutes ago Up 10 minutes my-app-container
Key Concept

If you remember nothing else from this pattern, remember: use a temporary container with volume mounts to safely backup and restore Docker volumes.

Common Mistakes
Trying to copy files directly from a running container without using volume mounts
This can miss data stored in volumes or cause inconsistent backups
Use a temporary container with --volumes-from or mount the volume directly to access all data
Not removing the temporary container after backup or restore
Leads to leftover containers cluttering your system
Use the --rm flag to automatically clean up temporary containers
Restoring backup files to the wrong volume or directory
Data will not be restored correctly and app may fail
Double-check volume names and paths before running restore commands
Summary
Use a temporary container with volume mounts to create a backup archive of your app data.
Verify your Docker volumes and containers before backup and restore operations.
Restore data by extracting the backup archive into the correct Docker volume using a temporary container.