Skip to content

Instantly share code, notes, and snippets.

@jgrodziski
Last active November 9, 2024 17:50
Show Gist options
  • Save jgrodziski/9ed4a17709baad10dbcd4530b60dfcbb to your computer and use it in GitHub Desktop.
Save jgrodziski/9ed4a17709baad10dbcd4530b60dfcbb to your computer and use it in GitHub Desktop.
Useful Docker Aliases
############################################################################
# #
# ------- 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: #
# 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 <container> #
# di <container> : docker inspect <container> #
# dim : docker images #
# dip : IP addresses of all running containers #
# dl <container> : docker logs -f <container> #
# 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> #
# #
############################################################################
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 $1 ${2:-bash}
}
function di-fn {
docker inspect $1
}
function dl-fn {
docker logs -f $1
}
function drun-fn {
docker run -it $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
}
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
@arthuraldridge
Copy link

Could those who use Docker extensively weigh in on rough weight of scripted/interactive use thus the utility of placing these aliases in .bashrc for consistency in syntax muscle memory? Does it make more sense to script docker commands long hand for portability across systems without automated config/versioning across a dozen or so systems?

@iolpltuisciaoln
Copy link

this is vital, especially when coupled with fzf:

personally, I do automatic configuration of the environment on every host like this:
sh -c "$(curl -fsSL https://raw.githubusercontent.com/iolpltuisciaoln/envup/master/envup.sh)" "" --unattended

@mspronk
Copy link

mspronk commented Aug 21, 2021

@jgrodziski, I have added functions to allow a few of the aliases to use partial ID or partial name, making things even easier.
I was going to create a merge request or pull request but wasn't clear on how to.
My fork is at https://gist.github.com/mspronk/cab0c175b876ef662e25b1ba2d22b069, feel free to use or merge it in.

PS credits for the inspiration of the additional function go to https://gist.github.com/akarzim

@alabotski
Copy link

How can I use this aliases with zsh?

@ZahirJ
Copy link

ZahirJ commented Mar 6, 2022

Hi, thank you for your contribution!
I would like to donate something I find useful, many times I use grep to filter images so I included this in my alias:

alias dim1="i=!:1 && docker images | grep $i "`

Example:
dim jenkins
is equal to `docker images | grep jenkins"

@td43
Copy link

td43 commented May 4, 2022

Does anyone know why if I run the alias drmid=drmid-fn in zsh to clean <none> images, I am getting:

Error: No such image: sha256:a55935bf5f1c144801678f4d723f8c3ffd041108284f69c76801d33562a11f98

But, if I copy/paste the command directly into the terminal it works?:

Deleted: sha256:a55935bf5f1c144801678f4d723f8c3ffd041108284f69c76801d33562a11f98

@td43
Copy link

td43 commented May 6, 2022

Does anyone know why if I run the alias drmid=drmid-fn in zsh to clean <none> images, I am getting:

Error: No such image: sha256:a55935bf5f1c144801678f4d723f8c3ffd041108284f69c76801d33562a11f98

But, if I copy/paste the command directly into the terminal it works?:

Deleted: sha256:a55935bf5f1c144801678f4d723f8c3ffd041108284f69c76801d33562a11f98

I have found a solution. It seems in bash/zsh if we use $(docker images -q -f dangling=true), the output is a string with break line "\n" and this causes the docker rmi not to read the full list of none images, e.g.

sha256:a55935bf5f1c144801678f4d723f8c3ffd041108284f69c76801d33562a11f98
sha256:a55935bf5f1c144801678f4d723f8c3ffd041108284f69c76801d33562a11f98
sha256:a55935bf5f1c144801678f4d723f8c3ffd041108284f69c76801d33562a11f98
sha256:a55935bf5f1c144801678f4d723f8c3ffd041108284f69c76801d33562a11f98
sha256:a55935bf5f1c144801678f4d723f8c3ffd041108284f69c76801d33562a11f98

But, if we use a function to run docker images -q -f dangling=true instead, then the alias works as expected since the output list in concatenated by \t

function noneimages {
       docker images --filter "dangling=true" -q --no-trunc
}

function docker-remove-none-images {
       [ ! -z "$(noneimages)" ] && docker rmi $(noneimages) || echo "no dangling images."
}

alias cdi=docker-remove-none-images

Then, the output is:

sha256:a55935bf5f1c144801678f4d723f8c3ffd041108284f69c76801d33562a11f98sha256:a55935bf5f1c144801678f4d723f8c3ffd041108284f69c76801d33562a11f98sha256:a55935bf5f1c144801678f4d723f8c3ffd041108284f69c76801d33562a11f98sha256:a55935bf5f1c144801678f4d723f8c3ffd041108284f69c76801d33562a11f98sha256:a55935bf5f1c144801678f4d723f8c3ffd041108284f69c76801d33562a11f98

And the none images are deleted.

@tkf144
Copy link

tkf144 commented Apr 19, 2023

Thanks all for submitting your aliases - useful stuff.

I am fond of Git's approach to aliases so I am using something like the below to achieve something similar for arbitrary CLI tools. Maybe someone will find it useful.

# I simply use alias d=__docker-wrapper which removes the need
# for `command docker` in the fn body, but for clarity, it's
# `docker` here.
alias docker=__docker-wrapper

__docker-wrapper() {
	if [[ "$1" == "rmid" ]]
	then
		command docker rmi $(command docker images --quiet --all --filter 'dangling=true')
	elif [[ "$1" == "cm" ]]
	then
		shift 1
		command docker compose "$@"
	else
		command docker "$@"
	fi
}

@j-antunes
Copy link

I see that this gist still uses docker-compose cmd line instead of docker compose, which I believe the lather is what is used now for docker. Should this gist be updated to reflect that?

@jgrodziski
Copy link
Author

@j-antunes thanks, yes I updated the gist with docker compose instead of docker-compose

@chadyred
Copy link

chadyred commented Nov 5, 2023

Hello, I edited and try your snippet, it is cool I add shortcut I love to use


#     dbash <container>: run a bash shell into a given container           #
#     dsh <container>: run a sh shell into a given container               #


function dbash-fn {
    docker exec -it $(docker ps -aqf "name=$1") bash;
}

function dsh-fn {
    docker exec -it $(docker ps -aqf "name=$1") sh;
}

alias dbash=dbash-fn
alias dsh=dsh-fn 

@krodelabestiole
Copy link

krodelabestiole commented Apr 30, 2024

Nice, thanks !

Some were the exact aliases I've been using for years on my own.

With two extras :

alias dcl='docker compose logs -f'

to display logs for a particular compose file.

and

function dupdate() {
	docker compose pull && docker compose build --no-cache
}

to update docker compose containers.

I don't get the use of aliases pointing to functions instead of giving the function the direct alias name ?
like so :

function drmid {
       imgs=$(docker images -q -f dangling=true)
       [ ! -z "$imgs" ] && docker rmi "$imgs" || echo "no dangling images."
}

and also why this kind of complicate things :

function dc-fn {
        docker compose $*
}
alias dc=dc-fn

instead of simply :

alias dc='docker compose'

There might be some reasons I don't get since I only use bash e.g. ?

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