Skip to content

Instantly share code, notes, and snippets.

@inakianduaga
Last active August 29, 2015 14:18
Show Gist options
  • Save inakianduaga/da95d7e86cb89f227972 to your computer and use it in GitHub Desktop.
Save inakianduaga/da95d7e86cb89f227972 to your computer and use it in GitHub Desktop.
Docker extended commands w/ autocompletion for bash
#!/usr/bin/env bash
# Docker extended commands
#
# `./docker` bash command wrapper to add additional functionality to the docker command, w/ autocompletion support (docker v1.6)
#
# Installation: Add this directly to `~/.bashrc` (or whatever your bash startup file is called) or save to a file and
# source it within `~/.bashrc`.
#
# Usage:
# docker wipe <container(s)/id(s)>: Gracefully stops and removes a container(s)
# docker bash <container/id>: Executes bash in an interactive shell on the given container (and sets TERM=xterm var)
# docker clean: Removes all stopped containers
#
# main docker command wrapper
#
docker() {
local DOCKER=/usr/bin/docker;
#
# Clean all stopped containers
#
if [[ $@ == "clean" ]]; then
local STOPPED_CONTAINERS=$($DOCKER ps -a | grep Exited | awk '{print $1}')
if [ -z "$STOPPED_CONTAINERS" ]; then
echo "No stopped containers"
else
echo "Removing all stopped containers:"
$DOCKER rm $STOPPED_CONTAINERS
fi
#
# Stop a container and remove it all at once
#
elif [ "$1" == "wipe" ]; then
shift
echo "Stopping container:"
$DOCKER stop "$@"
echo "Removing container:"
$DOCKER rm "$@"
#
# "Bash" into a given container
#
# https://github.com/docker/docker/issues/9299
# http://superuser.com/questions/344478/bash-execute-command-given-in-commandline-and-dont-exit
#
elif [ "$1" == "bash" ]; then
$DOCKER exec -it $2 bash -c "export TERM=xterm; exec bash"
#
# Fallback to default core docker
#
else
$DOCKER "$@"
fi
return $?;
}
#
# Add docker wipe autocompletion
#
_docker_wipe() {
__docker_containers_running
}
#
# Add docker bash autocompletion
#
_docker_bash() {
__docker_containers_running
}
#
# Add custom commands to the main docker command list autocompletion
# Based on v1.6 https://github.com/docker/docker/blob/v1.6.0/contrib/completion/bash/docker
#
_docker() {
local previous_extglob_setting=$(shopt -p extglob)
shopt -s extglob
local commands=(
attach
build
commit
cp
create
diff
events
exec
export
history
images
import
info
inspect
kill
load
login
logout
logs
pause
port
ps
pull
push
rename
restart
rm
rmi
run
save
search
start
stats
stop
tag
top
unpause
version
wait
#
# Custom commands below
#
wipe
clean
bash
)
local main_options_with_args="
--api-cors-header
--bip
--bridge -b
--default-ulimit
--dns
--dns-search
--exec-driver -e
--fixed-cidr
--fixed-cidr-v6
--graph -g
--group -G
--host -H
--insecure-registry
--ip
--label
--log-driver
--log-level -l
--mtu
--pidfile -p
--registry-mirror
--storage-driver -s
--storage-opt
--tlscacert
--tlscert
--tlskey
"
local main_options_with_args_glob=$(__docker_to_extglob "$main_options_with_args")
COMPREPLY=()
local cur prev words cword
_get_comp_words_by_ref -n : cur prev words cword
local command='docker' cpos=0
local counter=1
while [ $counter -lt $cword ]; do
case "${words[$counter]}" in
$main_options_with_args_glob )
(( counter++ ))
;;
-*)
;;
*)
command="${words[$counter]}"
cpos=$counter
(( cpos++ ))
break
;;
esac
(( counter++ ))
done
local completions_func=_docker_${command}
declare -F $completions_func >/dev/null && $completions_func
eval "$previous_extglob_setting"
return 0
}
@inakianduaga
Copy link
Author

updated autocompletitions for docker v1.6

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment