Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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)"
@djmaze

This comment has been minimized.

Copy link

@djmaze djmaze commented Oct 31, 2016

@schickling Thanks for the script! You probably want to remove the %3 here though.

@jcrben

This comment has been minimized.

Copy link

@jcrben jcrben commented Nov 13, 2016

Do you really need to back up the boot2docker.iso and disk.vmdk files? Can't you just store configuration?

@samuelcolvin

This comment has been minimized.

Copy link

@samuelcolvin samuelcolvin commented Jun 21, 2017

as @djmaze says the %3 breaks this on linux.

I'm guessing it's a mac thing? but unless it's essential would be good to remove.

@espoirMur

This comment has been minimized.

Copy link

@espoirMur espoirMur commented Jul 17, 2018

Thanks, find also the issue with %3 on Linux

on the new machine, I imported the configuration and run into this error, thanks to @g0blin79 for the solution.

docker-machine regenerate-certs dinghy

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment