diff --git a/plugins/docker/README.md b/plugins/docker/README.md index 0bc24b54a..77b1d6386 100644 --- a/plugins/docker/README.md +++ b/plugins/docker/README.md @@ -77,10 +77,16 @@ If you use Podman's Docker wrapper, you need to enable legacy completion. See ab | drs | `docker container restart` | Restart one or more containers | | dsta | `docker stop $(docker ps -q)` | Stop all running containers | | dstp | `docker container stop` | Stop one or more running containers | -| dsts | `docker stats` | Display real-time streaming statistics for containers | +| dsts | `docker stats` | Display real-time streaming statistics for containers | | dtop | `docker top` | Display the running processes of a container | | dvi | `docker volume inspect` | Display detailed information about one or more volumes | | dvls | `docker volume ls` | List all the volumes known to docker | | dvprune | `docker volume prune` | Cleanup dangling volumes | | dxc | `docker container exec` | Run a new command in a running container | | dxcit | `docker container exec -it` | Run a new command in a running container in an interactive shell | + +## Functions + +| Command | Description | +| :-------------------------- | :---------------------------------------------------------------------------------------------------- | +| `dxgcit ` | Run a new command in a running container selected via pattern matching on the image or container name | diff --git a/plugins/docker/docker.plugin.zsh b/plugins/docker/docker.plugin.zsh index 5268f6cd6..95a1a8312 100644 --- a/plugins/docker/docker.plugin.zsh +++ b/plugins/docker/docker.plugin.zsh @@ -36,6 +36,40 @@ alias dvprune='docker volume prune' alias dxc='docker container exec' alias dxcit='docker container exec -it' +function _dxgcit() { + if [ $# -lt 2 ]; then + echo "Usage: $0 " >&2 + return 1 + fi + + local pattern=$1 + shift 1 + + local -a candidates + candidates=( + ${(f)"$(docker ps --format "{{.ID}}:{{.Names}}:{{.Image}}" | grep -- "$pattern")"} + ) + if [ ${#candidates[@]} -eq 0 ]; then + printf "No container/image matches name pattern: '%s'!\n" "$pattern" >&2 + return 1 + fi + if [ ${#candidates[@]} -ne 1 ]; then + printf "Ambiguous container/image name pattern: '%s' results in '%d' candidates!\n" \ + "$pattern" "${#candidates[@]}" >&2 + for candidate in ${candidates[@]}; do + parts=(${(@s/:/)candidate}) + printf " %s (%s) (image: %s)\n" "${parts[1]}" "${parts[2]}" "${parts[3]}" >&2 + done + return 1 + fi + + local -a candidate + candidate=(${(@s/:/)${candidates[1]}}) + local id="${candidate[1]}" + docker container exec -it "$id" "$@" +} +alias dxgcit='_dxgcit' + if (( ! $+commands[docker] )); then return fi