Skip to content

Instantly share code, notes, and snippets.

@speelbarrow
Last active February 6, 2025 15:38
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!
docker() {
case "$1" in
disposable)
(shift 1; command docker run --rm -it --name disposable "$@")
;;
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 $(shift 1; printf "$@") $1
;;
*)
command docker "$@"
;;
esac
}
# zsh completions
function _docker-custom {
case $words[2] in
disposable)
export words=( "docker" "run" "${words[@]:2}" )
;;
*sh)
export words=( "docker" "exec" "-it" "${words[@]:2}" )
;;
esac
_docker
}
compdef _docker-custom docker
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment