Skip to content

Instantly share code, notes, and snippets.

@bakins
Created March 12, 2014 11:19
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 bakins/9504964 to your computer and use it in GitHub Desktop.
Save bakins/9504964 to your computer and use it in GitHub Desktop.
#!/bin/bash
VERSION=""
NAME=""
DIR=""
while getopts "v:n" opt; do
case $opt in
v)
VERSION=$OPTARG
;;
n)
NAME=$OPTARG
;;
d)
DIR=$OPTARG
;;
\?)
echo "Invalid option: -$OPTARG" >&2
exit
;;
esac
done
shift $(( OPTIND - 1 ))
DIR=$1
if [ -z "$DIR" ]; then
echo "dir is required" >&2
exit -2
fi
cd $DIR
if [ ! -f Dockerfile ]; then
echo "no Dockerfile" >&2
exit -3
fi
if [ -z "$VERSION" ]; then
VERSION=$(grep -E "^#\s*VERSION:" Dockerfile | cut -d ':' -f 2 | tr -d ' ')
if [ -z "$VERSION" ]; then
echo "version is required" >&2
exit -2
fi
fi
if [ -z "$NAME" ]; then
NAME=$(grep -E "^#\s*NAME:" Dockerfile | cut -d ':' -f 2 | tr -d ' ')
if [ -z "$NAME" ]; then
NAME=$(basename $DIR)
fi
fi
docker build --rm -t $NAME:$VERSION $DIR
docker inspect $NAME:$VERSION > $NAME-$VERSION.json
# this is a hack as you can only export containers, not images
# save does not do what you would expect it to do
ID=$(docker run -d $NAME:$VERSION "sleep 1")
sleep 3
docker export $ID | gzip -9 > $NAME-$VERSION.tar.gz
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment