Skip to content

Instantly share code, notes, and snippets.

@tlvince
Forked from schickling/_README.md
Created April 1, 2016 06:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tlvince/b4184984334936c7101610fbf9ba335b to your computer and use it in GitHub Desktop.
Save tlvince/b4184984334936c7101610fbf9ba335b to your computer and use it in GitHub Desktop.
Script to import and export docker-machine configurations to sync between hosts/collaborators

docker-machine import/export

Script to import and export docker-machine configurations to sync between hosts/collaborators

Export (on host 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

Import (on host 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

Note

This script requires you to have the same $MACHINE_STORAGE_PATH/certs available on all host systems

#! /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
#! /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)"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment