Skip to content

Instantly share code, notes, and snippets.

@gilchris
Last active February 14, 2024 05:45
Show Gist options
  • Save gilchris/3a8c1f4ac227a5314791de2460c31072 to your computer and use it in GitHub Desktop.
Save gilchris/3a8c1f4ac227a5314791de2460c31072 to your computer and use it in GitHub Desktop.
docker command shortcut
#!/bin/bash
function fzf_container() {
docker container ls -a | fzf -m | awk '{print $1}'
}
function fzf_running_container() {
docker ps | fzf -m | awk '{print $1}'
}
function container() {
case $1 in
inspect)
docker container inspect $(fzf_container)
;;
top)
docker container top $(fzf_container)
;;
command)
docker ps --no-trunc --format "{{.ID}} {{.Command}}" | grep ^$(fzf_container)
;;
log)
docker container logs $(fzf_running_container)
;;
*)
fzf_container
;;
esac
}
function fzf_image() {
docker images -a --digests | fzf -m | awk '{print $4}'
}
function image() {
case $1 in
rm)
fzf_image | xargs docker image rm -f
;;
clear)
docker rmi $(docker images -f "dangling=true" -q)
;;
*)
fzf_image
;;
esac
}
case $1 in
sh)
docker exec -it $(fzf_running_container) /bin/bash
;;
restart)
docker restart $(fzf_running_container)
;;
stop)
docker stop $(fzf_running_container)
;;
start)
docker start $(fzf_container)
;;
ps)
docker ps
;;
c)
container $2
;;
i)
image $2
;;
rmi)
image rm
;;
*)
cat << EOF
Usage: dkr COMMAND
Commands:
sh execute bash in running container
start start container
stop stop running container
restart restart running container
ps list of running containers
c list of all containers
c inspect detail info of running container
c top process info of running container
c command command of running container
c log container log
i list of all images
i rm remove image
i clear remove all dangling images
rmi remove image
EOF
;;
esac
@gilchris
Copy link
Author

, I added the network part too, and changed the default action to inspect ( container/network) instead of simple list. Nicely done here. Thank you very much

https://gist.github.com/cr3a7ure/ddb0cbb0efa9bd03760857a48e47ba27

Hi, your version is very nice. Thank you

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