为了在多个主机同步docker-machine的配置,常常需要对docker-machine配置进行导入导出。这里附上两个脚本分别用于导出/导入docker-machine配置:docker-machine-export.sh和docker-machine-import.sh。
这两个脚本需要所有的主机系统有相同的$MACHINE_STORAGE_PATH/certs。用法如下:
导出(主机 A)
$ docker-machine ls
NAME ACTIVE DRIVER STATE URL SWARM DOCKER ERRORS
dev - digitalocean Running tcp://example.com:2376 v1.10.1
$ ./docker-machine-export.sh dev
Exported machine to dev.zip
$ ls
docker-machine-import.sh
docker-machine-export.sh
dev.zip
使用docker-machine-export导出,给它一个导出的文件名,最后它会导出配置到zip压缩包。
导入 (主机 B)
$ docker-machine ls
NAME ACTIVE DRIVER STATE URL SWARM DOCKER ERRORS
$ ./docker-machine-import.sh dev.zip
Exported machine to dev.zip
$ docker-machine ls
NAME ACTIVE DRIVER STATE URL SWARM DOCKER ERRORS
dev - digitalocean Running tcp://example.com:2376 v1.10.1
导入在主机A的docker-machine的配置压缩包docker-machine-import.zip
脚本如下
docker-machine-export.sh
#! /bin/bash
if [ -z "$1" ]; then
echo "Usage: machine-export.sh MACHINE_NAME"
echo ""
echo "Exports the specified docker-machine to a MACHINE_NAME.zip file"
echo "Note: This script requires you to have the same \$MACHINE_STORAGE_PATH/certs available on all host systems"
exit 0
fi
machine_name=$1
docker-machine status $machine_name 2>&1 > /dev/null
if [ $? -ne 0 ]; then
echo "No such machine found"
exit 1
fi
set -e
MACHINE_STORAGE_PATH="${MACHINE_STORAGE_PATH:-"$HOME/.docker/machine"}"
machine_path="$MACHINE_STORAGE_PATH/machines/$machine_name"
tmp_path="/tmp/machine-export-$(date +%s%3)"
# copy to /tmp and strip out $MACHINE_STORAGE_PATH
mkdir -p $tmp_path
cp -r "$machine_path" "$tmp_path"
perl -pi -e "s|$MACHINE_STORAGE_PATH|__MACHINE__STORAGE_PATH__|g" $tmp_path/$machine_name/config.json
# create zip
rm -f "$machine_name.zip"
zip -rj "$machine_name.zip" "$tmp_path/$machine_name" > /dev/null
echo "Exported machine to $machine_name.zip"
# cleanup
rm -rf $tmp_path
docker-machine-import.sh
#! /bin/bash
set -e
if [ -z "$1" ]; then
echo "Usage: docker-machine-import.sh MACHINE_NAME.zip"
echo ""
echo "Imports an exported machine from a MACHINE_NAME.zip file"
echo "Note: This script requires you to have the same \$MACHINE_STORAGE_PATH/certs available on all host systems"
exit 0
fi
machine_archive="$1"
machine_name="${machine_archive/.zip/}"
MACHINE_STORAGE_PATH="${MACHINE_STORAGE_PATH:-"$HOME/.docker/machine"}"
machine_path="$MACHINE_STORAGE_PATH/machines/$machine_name"
if [ -d "$machine_path" ]; then
echo "$machine_name already exists"
exit 1
fi
rm -rf "$machine_name"
unzip "$machine_archive" -d "$machine_name" > /dev/null
perl -pi -e "s|__MACHINE__STORAGE_PATH__|$MACHINE_STORAGE_PATH|g" $machine_name/config.json
mv "$machine_name" "$MACHINE_STORAGE_PATH/machines"
echo "Imported $machine_name to docker-machine ($machine_path)"
参考:https://gist.github.com/schickling/2c48da462a7def0a577e