Skip to content

Instantly share code, notes, and snippets.

@cr3a7ure
Forked from gilchris/dkr
Created January 9, 2023 17:46
Show Gist options
  • Save cr3a7ure/ddb0cbb0efa9bd03760857a48e47ba27 to your computer and use it in GitHub Desktop.
Save cr3a7ure/ddb0cbb0efa9bd03760857a48e47ba27 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
l)
fzf_container
;;
top)
docker container top $(fzf_container)
;;
command)
docker ps --no-trunc --format "{{.ID}} {{.Command}}" | grep ^$(fzf_container)
;;
*)
docker container inspect $(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
}
function fzf_network() {
docker network ls | fzf -m | awk '{print $2}'
}
function network() {
case $1 in
rm)
fzf_network | xargs docker network rm
;;
dc)
fzf_network | xargs docker network disconnect
;;
l)
fzf_network
;;
use)
fzf_network | xargs docker network inspect | grep Name | tail -n +2 | cut -d':' -f2 | tr -d ',\"'
;;
*)
fzf_network | xargs docker network inspect
;;
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
;;
n)
network $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 detail info of running container
c l list of all containers
c top process info of running container
c command command of running container
n inspect network
n dc disconnect network from containers
n rm remove network
n use display containers linked to the network
n l list all networks
i list of all images
i rm remove image
i clear remove all dangling images
rmi remove image
EOF
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment