Skip to content

Instantly share code, notes, and snippets.

@gianpaolof
Forked from cjus/.docker_aliases
Last active March 19, 2021 15:14
Show Gist options
  • Save gianpaolof/60ef3df5a1062860b08e7cef26304a74 to your computer and use it in GitHub Desktop.
Save gianpaolof/60ef3df5a1062860b08e7cef26304a74 to your computer and use it in GitHub Desktop.
Docker aliases
#!/bin/sh
alias dm='docker-machine'
alias dmx='docker-machine ssh'
alias dk='docker'
alias dki='docker images'
alias dks='docker service'
alias dkrm='docker rm'
alias dkl='docker logs'
alias dklf='docker logs -f'
alias dkflush='docker rm $(docker ps --no-trunc -aq)'
alias dkflush2='docker rmi $(docker images --filter "dangling=true" -q --no-trunc)'
alias dkt='docker stats --format "table {{.Name}}\t{{.CPUPerc}}\t{{.MemUsage}}\t{{.NetIO}}"'
alias dkps="docker ps --format '{{.ID}} ~ {{.Names}} ~ {{.Status}} ~ {{.Image}}'"
dkln() {
docker logs -f $(docker ps | grep $1 | awk '{print $1}')
}
# Remove all docker containers running and exited
alias drma='__drma() { docker ps -aq "$@" | xargs -r docker rm -f; }; __drma'
# Remove all docker images
alias drmia='__drmia() { docker images -q "$@" | xargs -r docker rmi -f; }; __drmia'
# Remove all custom docker networks
alias drmnet='__drmnet() { docker network ls -q -f type=custom "$@" | xargs -r docker network rm; }; __drmnet'
# Remove all unused volumes
alias drmvol='__drmvol() { docker volume ls -q "$@" | xargs -r docker volume rm; }; __drmvol'
# Remove all docker containers and all docker images
alias drmall='docker-rma && docker-rmia'
# Remove all docker containers, images, custom networks, and volumes
alias duke='docker-rmall; docker-rmnet; docker-rmvol'
# Remove only exited containers, unused images, unused networks, and unused volumes
alias dclean='docker-rma -f status=exited; docker-rmia -f dangling=true; docker-rmnet; docker-rmvol -f dangling=true'
# Bash into running container (https://github.com/tcnksm/docker-alias)
dbash() { docker exec -it $(docker ps -aqf "name=$1") bash; }
#https://looselytyped.com/blog/2018/02/07/docker-tip-1-docker-aliases/
dtags() {
local image="${1}"
curl -s -S "https://registry.hub.docker.com/v2/repositories/library/${image}/tags/" \
| jq '."results"[]["name"]' \
| sort
}
# Get container IP
alias dip="docker inspect --format '{{ .NetworkSettings.IPAddress }}'"
dktop() {
docker stats --format "table {{.Container}}\t{{.Name}}\t{{.CPUPerc}} {{.MemPerc}}\t{{.NetIO}}\t{{.BlockIO}}"
}
dkstats() {
if [ $# -eq 0 ]
then docker stats --no-stream;
else docker stats --no-stream | grep $1;
fi
}
dke() {
docker exec -it $1 /bin/sh
}
dkexe() {
docker exec -it $1 $2
}
dkstate() {
docker inspect $1 | jq .[0].State
}
dksb() {
docker service scale $1=0
sleep 2
docker service scale $1=$2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment