Skip to content

Instantly share code, notes, and snippets.

@mspronk
Forked from jgrodziski/docker-aliases.sh
Last active September 6, 2023 15:08
Show Gist options
  • Save mspronk/cab0c175b876ef662e25b1ba2d22b069 to your computer and use it in GitHub Desktop.
Save mspronk/cab0c175b876ef662e25b1ba2d22b069 to your computer and use it in GitHub Desktop.
Useful Docker Aliases
function help-fn {
echo "`cat <<EOF
############################################################################
# #
# ------- Useful Docker Aliases -------- #
# #
# # Installation : #
# copy/paste these lines into your .bashrc or .zshrc file or just #
# type the following in your current shell to try it out: #
# wget -O - https://gist.githubusercontent.com/jgrodziski/9ed4a17709baad10dbcd4530b60dfcbb/raw/d84ef1741c59e7ab07fb055a70df1830584c6c18/docker-aliases.sh | bash
# #
# # Usage: #
# dhelp : this help output #
# daws <svc> <cmd> <opts> : aws cli in docker with <svc> <cmd> <opts> #
# dc : docker-compose #
# dcu : docker-compose up -d #
# dcd : docker-compose down #
# dcr : docker-compose run #
# dex <container>: execute a bash shell inside the RUNNING <partial> #
# di <container> : docker inspect <partial> #
# dim : docker images #
# dip : IP addresses of all running containers #
# dl <container> : docker logs -f <partial> #
# dnames : names of all running containers #
# dps : docker ps #
# dpsa : docker ps -a #
# drmc : remove all exited containers #
# drmid : remove all dangling images #
# drun <image> : execute a bash shell in NEW container from <image> #
# dsr <container>: stop then remove <container> #
# #
# <partial> is either a partial container id, or name #
# <container> is an exact container id #
############################################################################
EOF`"
}
function dnames-fn {
for ID in `docker ps | awk '{print $1}' | grep -v 'CONTAINER'`
do
docker inspect $ID | grep Name | head -1 | awk '{print $2}' | sed 's/,//g' | sed 's%/%%g' | sed 's/"//g'
done
}
function dip-fn {
echo "IP addresses of all named running containers"
for DOC in `dnames-fn`
do
IP=`docker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}} {{end}}' "$DOC"`
OUT+=$DOC'\t'$IP'\n'
done
echo -e $OUT | column -t
unset OUT
}
function dex-fn {
docker exec -it $(dkps! $1) ${2:-bash}
}
function di-fn {
docker inspect $(dkps! $1)
}
function dl-fn {
docker logs -f $(dkps! $1)
}
function drun-fn {
docker run -it $(dkps! $1) $2
}
function dcr-fn {
docker-compose run $@
}
function dsr-fn {
docker stop $1;docker rm $1
}
function drmc-fn {
docker rm $(docker ps --all -q -f status=exited)
}
function drmid-fn {
imgs=$(docker images -q -f dangling=true)
[ ! -z "$imgs" ] && docker rmi "$imgs" || echo "no dangling images."
}
# in order to do things like dex $(dlab label) sh
function dlab {
docker ps --filter="label=$1" --format="{{.ID}}"
}
function dc-fn {
docker-compose $*
}
function d-aws-cli-fn {
docker run \
-e AWS_ACCESS_KEY_ID=$AWS_ACCESS_KEY_ID \
-e AWS_DEFAULT_REGION=$AWS_DEFAULT_REGION \
-e AWS_SECRET_ACCESS_KEY=$AWS_SECRET_ACCESS_KEY \
amazon/aws-cli:latest $1 $2 $3
}
function dkps! {
if [ ! -z "$1" ]; then
{ docker ps -f "id=$1" -f "status=running" -q && docker ps -f "name=$1" -f "status=running" -q } | head -n 1
fi
}
alias dhelp=help-fn
alias daws=d-aws-cli-fn
alias dc=dc-fn
alias dcu="docker-compose up -d"
alias dcd="docker-compose down"
alias dcr=dcr-fn
alias dex=dex-fn
alias di=di-fn
alias dim="docker images"
alias dip=dip-fn
alias dl=dl-fn
alias dnames=dnames-fn
alias dps="docker ps"
alias dpsa="docker ps -a"
alias drmc=drmc-fn
alias drmid=drmid-fn
alias drun=drun-fn
alias dsp="docker system prune --all"
alias dsr=dsr-fn
@nomandera
Copy link

Nice fork. Thanks for the work.

Is the dkps funtion shell specific (zsh perhaps)?

bash seems to not like the {} bracing and since the function is used so often it has quite a big impact.

@cybersholt
Copy link

cybersholt commented Sep 6, 2023

@nomandera here's a fix for the dkps function, I had ChatGPT repair it, seems to work well with: Ubuntu 22.04.3 LTS x86_64 / Kernel: 6.2.0-32-generic

function dkps! {
  if [ ! -z "$1" ]; then
    { docker ps -f "id=$1" -f "status=running" -q && docker ps -f "name=$1" -f "status=running" -q; } | head -n 1
  fi
}

Changes made:

Added a missing ; before the closing } of the command group ({ ... }).
Removed the extra space before the if keyword.
This corrected code should work without syntax errors. It checks if there's a non-empty argument ($1) and then runs the specified Docker commands, piping the output through head -n 1.

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