Jul 092016
Since moving Docker containers is easy, and Amazon keeps on patching and rebooting my little AWS server, time to put the theory into practice and move to some other place.
Moving was 4 parts:
- Copy the data volumes over
- Change DNS
- Configure the load balancer
- Start everything in the new place
The first one needed some scripting since moving data containers in Docker is not a built-in function. And that needs a corresponding restore function of course. Here the scripts, first backup:
#!/bin/bash # Does a backup from one or many Docker volumes # Harald Kubota 2016-07-09 if [[ $# -eq 0 ]] ; then echo "Usage: $0 docker_volume_1 [docker_volume_2 ...]" echo "will create tar.bz2 files of the docker volumes listed" exit 10 fi today=`date +%Y-%m-%d` for vol in "$@" ; do backup_file=$vol-${today}.tar.bz2 echo "Backup up docker volume $i into $backup_file" docker run --rm -v $vol:/voldata -v $(pwd):/backup debian:8 \ tar cf /backup/$vol.tar.tmp /voldata bzip2 <$vol.tar.tmp >$backup_file && rm -f $vol.tar.tmp done
and here restore:
#!/bin/bash # Restore a tar dump back into a Docker data volume # tar file can be .tar or .tar.bz2 # Name is always VOLUME-NAME-20YY-MM-DD.tar[.bz2] # Harald Kubota 2016-07-09 if [[ $# -ne 1 ]] ; then echo "Usage: $0 tarfile" echo "will create a new volume derived from the tarfile name and restore the tarfile data into it" exit 10 fi today=`date +%Y-%m-%d` for i in "$@" ; do volumename=$(echo $i | sed 's/\(.*\)-20[0-9][0-9]-[0-9][0-9]-[0-9][0-9]\.tar.*/\1/') docker volume create --name $volumename # if .bz2 then decompress first length=${#i} last4=${i:length-4} tar_name=$i delete_tar=0 echo "Restoring tar file $i into volume $volumename" if [[ ${last4} == ".bz2" ]] ; then tar_name=$(basename $i .bz2) bunzip2 <$i >$tar_name delete_tar=1 fi #echo "tar_name=$tar_name, delete_tar=$delete_tar" docker run --rm -v $volumename:/voldata -v $(pwd):/backup debian:8 \ tar xfv /backup/$tar_name if [[ $delete_tar -eq 1 ]] ; then rm $tar_name fi done
With this, moving Docker volumes is a piece of cake, and it doubles as a universal backup mechanism too.
Updating DNS is trivial, as is the Load Balancer part which maps the popular port 80 to the correct Docker instance. Starting is a simple “docker-compose up”.