Skip to content

Instantly share code, notes, and snippets.

@sbimikesmullin
Last active January 24, 2017 17:52
Show Gist options
  • Save sbimikesmullin/6a1e5131a457cfff5029b4399a46258e to your computer and use it in GitHub Desktop.
Save sbimikesmullin/6a1e5131a457cfff5029b4399a46258e to your computer and use it in GitHub Desktop.
Aliases wrapper makes simpler interface for Docker CLI
#!/usr/bin/env bash
#
# Whale: a simpler interface for docker cli
# LICENSE: GPLv3
#
# - `whale build` # builds Dockerfile in current directory
# - `whale run` # runs last `whale build` container
# - `whale buildrun` # same as `whale build && whale run`
# - `whale sh` # opens `/bin/ash` on last `whale build` container (you can optionally specify another shell like `/bin/bash` as the first argument)
# - `whale buildrun -p 8080:80` # build and run the container with options
LAST_HASH=$(cat /tmp/.LAST_WHALE_BUILD)
VERB="${1}"; shift
OPTIONAL_HASH=$(echo "${1}" | sed -n -r 's/^([a-z0-9]+)$/\1/p'); if [ -n "${OPTIONAL_HASH}" ]; then shift; else unset OPTIONAL_HASH; fi
case "${VERB}" in
build)
CMD="docker build ."
echo + ${CMD}
exec 6>&1; OUT=$(exec ${CMD}| tee /dev/fd/6)
CODE=$?
LAST_HASH=$(echo "${OUT}" | sed -n -r 's/Successfully built ([a-z0-9]+)/\1/p')
echo "${LAST_HASH}" > /tmp/.LAST_WHALE_BUILD
exit ${CODE}
;;
run)
set -x
docker run $* ${OPTIONAL_HASH:-$LAST_HASH}
;;
sh)
OPTIONAL_SHELL="${1}"
shift
set -x
docker run -it $* ${OPTIONAL_HASH-$LAST_HASH} ${OPTIONAL_SHELL:-"/bin/ash"}
;;
buildrun)
whale build && whale run $*
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment