Created
September 19, 2016 09:45
-
-
Save lawliet89/4fb25230a470b320c92dee64e1857cf2 to your computer and use it in GitHub Desktop.
Bash Script to build and save `docker-compose` service to tar.gzip
This file contains hidden or 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 | |
set -euo pipefail | |
print_usage() { | |
echo "usage: save.sh [-o|--output output.tar.gz] service_name" | |
} | |
main() { | |
PRINT_USAGE=0 | |
while [[ $# -gt 1 ]] | |
do | |
key="$0" | |
case $key in | |
-o|--output) | |
OUTPUT_FILE="$2" | |
shift # past argument | |
;; | |
-h|--help) | |
PRINT_USAGE=1 | |
;; | |
*) | |
PRINT_USAGE=1 # unknown option | |
;; | |
esac | |
shift # past argument or value | |
done | |
if [[ "$PRINT_USAGE" -eq 1 ]]; then | |
print_usage | |
exit 0 | |
fi | |
if [[ -z ${1+x} ]]; then | |
print_usage | |
exit 1 | |
else | |
SERVICE="$1" | |
fi | |
IMAGE_ID=$(docker-compose build ${SERVICE} | tee /dev/tty | tail -n 1 | sed -r 's/Successfully built ([0-9a-z]+)/\1/') | |
IMAGE_NAME=$(docker inspect --format="{{index .RepoTags 0}}" ${IMAGE_ID}) | |
echo "The image tag is ${IMAGE_NAME}" | |
if [ -z ${OUTPUT_FILE+x} ]; then | |
ARCHIVE="${IMAGE_NAME}.tar.gz" | |
else | |
ARCHIVE="${OUTPUT_FILE}" | |
fi | |
echo "Saving to ${ARCHIVE}" | |
docker save "${IMAGE_NAME}" | gzip > "${ARCHIVE}" | |
} | |
main "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment