Last active
June 17, 2022 15:20
-
-
Save kimh/79f7bcb195466acea39a to your computer and use it in GitHub Desktop.
Shell script to demonstrate docker migration with CRIU
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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
Hello , I want to know whether this shell can realize docker live migration?
I can not find the command docker "docker restore" in line 64