Skip to content

Instantly share code, notes, and snippets.

@speelbarrow
Last active March 16, 2024 15:59
Show Gist options
  • Save speelbarrow/3f89da79bb7245516aafe3a9b94919f8 to your computer and use it in GitHub Desktop.
Save speelbarrow/3f89da79bb7245516aafe3a9b94919f8 to your computer and use it in GitHub Desktop.
Docker convenience command extensions
# Source this file!
function docker {
case "$1" in
disposable)
command docker run --rm -it --name disposable ${@[@]:2}
;;
;;
images-grep)
command docker images -a --format 'table {{.Repository}}:{{.Tag}} {{.ID}}' | tail -n +2 | sed -nE "s/^$2.* ([A-z0-9]+)$/\\1/p"
;;
ps-grep)
command docker ps -a --format 'table {{.Names}} {{.ID}}' | tail -n +2 | sed -nE "s/^$2.* ([A-z0-9]+)$/\\1/p"
;;
rmi-grep)
command docker rmi $(docker images-grep $2)
;;
rm-grep)
command docker rm $(docker ps-grep $2)
;;
*sh)
if ! command docker ps --format '{{.Names}}' | grep -xq "$2"; then
if ! command docker start $2; then
return 1
fi
fi
command docker exec -it ${@[@]:2} $1
;;
*)
command docker "$@"
;;
esac
}
# ZSH completions
if [ -n "$($SHELL -c 'echo $ZSH_VERSION')" ]; then
function _docker-custom {
case $words[2] in
disposable)
export words=( "docker" "run" "${words[@]:2}" )
;;
*sh)
export words=( "docker" "exec" "-it" "${words[@]:2}" )
;;
esac
if [ "${words[2]}" = "disposable" ]; then
export words=( "docker" "run" ${words[@]:2} )
fi
_docker
}
compdef _docker-custom docker
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment