Migrate Docker Containers

Welcome!

Also, It is always good to know how to actualy backup your persisted data, isn’t it?

You will want to have a solution that allows to keep a could of backups of each volume for at least a couple of days.

Once all containers are stopped, something like this should be sufficient to backup all volumes:

for volume in $(docker volume ls -q); do
   docker run -ti --rm -v $volume:/source -v ./backups:/backups alpine bash -c 'cd /source; tar czvf /backups/$volume.tar.gz .'
 done

And this to create them again on the new host:

 for backup in $(ls -1 backups); do
    docker create volume ${backup%%.tar.gz}
    docker run -ti --rm -v ${backup%%.tar.gz}:/target -v ./backups:/backups alpine bash -c 'cd /target; tar xzvf /backups/$backup'
 don 

Update: fix of the backup snippet, as it missed an important . at the end of the tar czcf line.

2 Likes