Skip to content

Instantly share code, notes, and snippets.

@flungo
Last active February 12, 2024 19:23
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save flungo/6b5f607db87c3c034609c8dbc5b40966 to your computer and use it in GitHub Desktop.
Save flungo/6b5f607db87c3c034609c8dbc5b40966 to your computer and use it in GitHub Desktop.
Script for sending docker images between machines using SSH
#!/bin/bash
# MIT License
#
# Copyright (c) 2017 Fabrizio Lungo <opensource@flungo.me>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
# Script for sending docker images between machines using SSH
usage() {
cat <<EOF
Usage: $0 [-s] image [ssh_opts] [user@]hostname
$0 -h
Sends a docker image to a remote host using SSH.
-h Show this help message
-s Use sudo on remote
image The name of the docker image to send
ssh_opts Additional SSH options for connecting, see \`man ssh\`
user The user to connect to the remote host as
hostname The hostname of the remote machine
EOF
}
SUDO=""
# Parse the flags for this script
while [ "${1:0:1}" == "-" ]; do
case "$1" in
-h)
usage
exit 0
;;
-s)
SUDO="sudo"
;;
*)
>&2 echo "Unrecognised flag: $1"
>&2 echo
>&2 usage
exit 1
;;
esac
shift
done
# At least 2 arguments should be left
if [ $# -lt 2 ]; then
>&2 echo "Not enough arguments"
>&2 echo
>&2 usage
exit 1
fi
# Get the name of the image to send
IMAGE="$1"
shift
# Send the image using bzip compression and showing a progress bar
# See https://stackoverflow.com/a/26226261/2742221
docker save "$IMAGE" | bzip2 | pv | ssh "$@" "bunzip2 | $SUDO docker load"
@flungo
Copy link
Author

flungo commented Aug 4, 2017

TODO

  • Support other compression formats: no compression, gzip
  • Flag to reverse the direction of transfer: -r

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