Course contentsShow
← Docker, Kubernetes & CI/CD
Lesson 196 of 8023. Docker Orchestration and StorageFree lesson

Backing Up and Restoring Container Volumes

Learners will execute data migration strategies by creating archives of volume contents to ensure business continuity and disaster recovery readiness.

In this lesson, you will learn how to safeguard your application’s state by creating portable archives of your named volume data to ensure you can recover from potential data loss.

The Strategy: "Sidecar" Archiving

When you use named volumes to persist data, that data lives in a specific directory on the host managed by Docker. To back it up, you don’t need to shut down your entire application. Instead, you can run a temporary "utility" container that mounts the volume you want to back up alongside a local directory on your host.

By executing a tar command inside this temporary container, you can compress the contents of the volume into a single archive file (like backup.tar.gz) directly on your host machine.

How to Create a Backup

Imagine you have a volume named db_data and you want to save it to your current folder. You can run this command:

docker run --rm -v db_data:/source -v $(pwd):/backup alpine \
  tar -cvzf /backup/db_backup.tar.gz -C /source .

Here is what is happening:

  • --rm: Automatically removes the temporary container once the backup is finished.
  • -v db_data:/source: Mounts your persistent volume to a folder inside the container.
  • -v $(pwd):/backup: Mounts your current host directory so the container can write the file to your computer.
  • tar -cvzf: Instructs the container to zip up the contents of /source and place it in the /backup folder.

How to Restore

Restoration is simply the reverse process. You create a new volume (or use an existing one), mount it as the destination, and extract your archive into it:

docker run --rm -v db_data:/target -v $(pwd):/backup alpine \
  tar -xvzf /backup/db_backup.tar.gz -C /target

By keeping these archives off-site or in version-controlled storage, you ensure that even if a host fails or a volume is accidentally deleted, your business-critical data can be redeployed quickly to a fresh environment. This process is a foundational step in disaster recovery, ensuring your persistent data survives beyond the lifecycle of a single container deployment.

Key Takeaway: Backing up volumes involves using a temporary utility container to archive the data from a named volume into a portable file on your host, which can then be extracted to restore the environment.