Create a gist now

Instantly share code, notes, and snippets.

Shell script to demonstrate docker migration with CRIU
#!/bin/bash -e
function run-vg-cmd() {
pushd $1
eval $2
popd
}
function usage() {
echo "Usage: $0 container from-vagrant-dir to-vagrant-dir"
}
if [ $# -eq 0 ]
then
echo "$0: Migrating a Docker container from one vagrant host to another"
echo ""
usage
exit 1
fi
workdir="/tmp/docker-migration"
container=$1
vagrant_from=$2
vagrant_to=$3
if test "${container}" = ""; then
echo 'container is required'
echo ""
usage
exit 1
fi
if test "${vagrant_from}" = ""; then
echo 'vagrant-from-dir is required'
echo ""
usage
exit 1
fi
if test "${vagrant_to}" = ""; then
echo 'vagrant-to-dir is required'
echo ""
usage
exit 1
fi
# Cleanup first
rm -rf $workdir && mkdir -p $workdir && cd $workdir
run-vg-cmd $vagrant_from "vagrant ssh -- sudo rm -rf /tmp/$container"
run-vg-cmd $vagrant_to "vagrant ssh -- sudo rm -rf /tmp/$container"
# Dump ssh config for scp
run-vg-cmd $vagrant_from "vagrant ssh-config > ssh.config"
run-vg-cmd $vagrant_to "vagrant ssh-config > ssh.config"
# Checkpoint a container
run-vg-cmd $vagrant_from "vagrant ssh -- docker checkpoint --image-dir=/tmp/$container $container"
# Copy container dump
run-vg-cmd $vagrant_from "scp -F ssh.config -r vagrant@default:/tmp/$container $workdir/$container"
run-vg-cmd $vagrant_to "scp -F ssh.config -r $workdir/$container vagrant@default:/tmp/$container"
# Restore container from dump
run-vg-cmd $vagrant_to "vagrant ssh -- 'docker rm -f $container >/dev/null 2>&1; docker restore --force=true --image-dir=/tmp/$container \`docker create --name=$container busybox\`'"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment