How to back up Docker volumes and containers?

Published on Aug. 22, 2023, 12:19 p.m.

To back up Docker volumes and containers, you can use various methods. Here are a few examples:

  1. Using a backup container:
    You can spin up a new container with the same volume as the container you want to back up and execute the tar or rsync command to create an archive of the data. Here’s an example command:
docker run --rm --volumes-from mycontainer -v $(pwd):/backup ubuntu tar czvf /backup/mycontainer-backup.tar.gz /data

This command will create a compressed tarball archive of the “data” directory in the “mycontainer” container.

  1. Using a volume snapshot:
    You can use the Docker CLI to create a snapshot of the volume you want to back up, and then create a new volume from the snapshot. Here’s an example command:
docker volume create --driver vieux/sshfs --name mybackup -o [email protected]:/path/to/backups -o password=mypassword --from mysource

This command creates a new volume called “mybackup” that is a snapshot of the “mysource” volume. The “mybackup” volume is created using the vieux/sshfs driver and mounted at the specified SSH location.

  1. Using a third-party backup tool:
    You can use a third-party backup tool, such as Duplicity or Bacula, to back up your Docker volumes and containers. These tools provide advanced backup features such as encryption and incremental backups.

These are just a few examples of the many methods available for backing up Docker volumes and containers. You should choose the method that best fits your specific use case and requirements.

How to restore Docker volumes and containers from backup?