Skip to content

Instantly share code, notes, and snippets.

@lgs
Forked from proppy/README.md
Created February 1, 2014 11:44
Show Gist options
  • Save lgs/8751200 to your computer and use it in GitHub Desktop.
Save lgs/8751200 to your computer and use it in GitHub Desktop.

docker.sh: a small interactive shell hack for docker

Usage: ./docker.sh [base-image-name]

Demo: http://ascii.io/a/5011

#!/bin/bash
DEBUG=${DEBUG:+-x}
export IMG=${1:-ubuntu}
DOCKERRC=$(dirname $0)/dockerrc
bash --rcfile ${DOCKERRC} ${DEBUG}
shopt -s extdebug
alias docker=lxc-docker
DIRTY="*"
CLEAN=""
STATE=${CLEAN}
PS1="docker@\${REPO}:\${IMG}!\${ID}\${STATE}> "
dockerenv()
{
CMD=$(history 1 | sed -e "s/^[ ]*[0-9]*[ ]*//g")
set ${CMD}
case "$1" in
!*)
shift
eval $@
;;
from)
IMG=$(docker images -q=true $2 | head -n 1)
IMG=${IMG:-$2}
;;
to)
REPO=$2
;;
pop)
LAST=$(docker history "${IMG}" | sed -n 3p)
echo rewinding to ${LAST}
IMG=$(echo "${LAST}" | cut -d' ' -f1 | cut -d':' -f1)
STATE=${DIRTY}
;;
put)
echo commiting $(docker ps -l | sed -n 2p)
IMG=$(docker commit "${ID}" ${REPO:-$2})
REPO=${2:-${REPO}}
STATE=${CLEAN}
;;
h)
docker history "${IMG}"
;;
enter)
docker run -i -t "${IMG}" bash
ID=$(docker ps -l -q)
;;
help)
docker ${CMD}
echo "Shortcuts:"
echo " from <img> Set current image."
echo " to <repo> Set destination repo."
echo " put [repo] Commit last command to a new image."
echo " pop Go back to previous image."
echo " h Print current image history."
echo " enter Execute an interactive shell in the container."
;;
*)
docker help $1 2>&1 | grep "Command not found" > /dev/null
if [ $? -eq 0 ]
then
docker run "${IMG}" $(eval echo ${CMD})
ID=$(docker ps -l -q)
STATE=${DIRTY}
else
docker $(eval echo ${CMD})
fi
;;
esac
return 1
}
trap dockerenv DEBUG
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment