Last active
January 29, 2019 13:51
-
-
Save cmizzi/c23d87eb4eb978ab7b102df9503bc258 to your computer and use it in GitHub Desktop.
Add `dockershell` command to get logged in container (service or container)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| dockershell() { | |
| if [ "$1" = "service" ]; then | |
| ID=$(docker inspect --format '{{.Status.ContainerStatus.ContainerID}}' $(docker service ps -q "$2" | head -1)) | |
| else | |
| ID=$2 | |
| fi; | |
| docker exec -t -i -e PS1="$ID:\w# " "$ID" bash --noprofile --norc | |
| } | |
| __dockershell() { | |
| local state | |
| _arguments \ | |
| "1: :->kind" \ | |
| "*: :->containers" \ | |
| case $state in | |
| (kind) compadd "$@" service container ;; | |
| (*) | |
| if [ "$words[2]" = "service" ]; then | |
| _arguments "*:services:($((docker info 2> /dev/null | grep -i 'swarm: active') && docker service ls -q --format '{{.Name}}'))" | |
| else | |
| _arguments "*:containers:($(docker ps -q --format '{{.Names}}'))" | |
| fi; | |
| ;; | |
| esac | |
| } | |
| compdef __dockershell dockershell |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A new version with "servicelike" to use service name when not in a swarm manager :