Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
@jgrodziski

This comment has been minimized.

Copy link
Owner Author

@jgrodziski jgrodziski commented Nov 3, 2016

By the way, feel free to submit your own aliases, these are the docker commands I use the most.

@iMilnb

This comment has been minimized.

Copy link

@iMilnb iMilnb commented Oct 3, 2018

Thanks for those useful aliases! Please note that in the drmid alias, the space before docker makes the command fail at least on zsh.

@iMilnb

This comment has been minimized.

Copy link

@iMilnb iMilnb commented Feb 20, 2019

Here's a couple of modifications that work well with zsh, fixes drmid, makes dex more flexible around shell and adds a new dlab alias:

--- tmp.alias   2019-02-20 08:56:00.893600651 +0100
+++ /home/imil/.docker_aliases  2019-02-20 08:57:13.364311360 +0100
@@ -43,7 +43,7 @@
 }

 function dex-fn {
-       docker exec -it $1 /bin/bash
+       docker exec -it $1 ${2:-bash}
 }

 function di-fn {
@@ -55,13 +55,27 @@
 }

 function drun-fn {
-       docker run -it $1 /bin/bash
+       docker run -it $1 $2
 }

 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}}"
+}
+
 alias dcu="docker-compose up -d"
 alias dcd="docker-compose down"
 alias dex=dex-fn
@@ -72,7 +86,7 @@
 alias dnames=dnames-fn
 alias dps="docker ps"
 alias dpsa="docker ps -a"
-alias drmc="docker rm $(docker ps --all -q -f status=exited)"
-alias drmid="docker rmi $( docker images -q -f dangling=true)"
+alias drmc=drmc-fn
+alias drmid=drmid-fn
 alias drun=drun-fn
-alias dsr=dsr-fn
\ No newline at end of file
+alias dsr=dsr-fn
@iolpltuisciaoln

This comment has been minimized.

Copy link

@iolpltuisciaoln iolpltuisciaoln commented May 27, 2020

outputs aligned columns

function dip-fn {
    echo "IP addresses of all named running containers"

    for DOC in `dnames-fn`
    do
        IP=`docker inspect $DOC | grep -m3 IPAddress | cut -d '"' -f 4 | tr -d "\n"`
        OUT+=$DOC'\t'$IP'\n'
    done
    echo $OUT|column -t
}

thanx

@stefan-matic

This comment has been minimized.

Copy link

@stefan-matic stefan-matic commented Jun 21, 2020

function dc-fn {
        docker-compose $*
}

alias dc='dc-fn'

useful when running "docker-compose" commands inside the container folder like

dc logs --tail=100 instead of docker-compose logs --tail=100
or
dc ps instead of docker-compose ps for status check

@egdavid

This comment has been minimized.

Copy link

@egdavid egdavid commented Jul 21, 2020

function dcr-fn {
	docker-compose run $@
}
alias dcr=dcr-fn
@jgrodziski

This comment has been minimized.

Copy link
Owner Author

@jgrodziski jgrodziski commented Jul 21, 2020

Thank you to all the contributors (@egdavid, @stefan-matic, @iolpltuisciaoln, @iMilnb) with your useful fix and additions to these aliases, I've finally revised the gist with all your contributions

@mi6th

This comment has been minimized.

Copy link

@mi6th mi6th commented Oct 19, 2020

For the record, the dc alias may conflict with the GNU/Linux arbitrary precision calculator dc.

@noplanman

This comment has been minimized.

Copy link

@noplanman noplanman commented Mar 18, 2021

Running dip consecutively duplicates the entries, so I had to unset the OUT variable at the end.
List all IPs of a container using docker inspect --format flag.
Also, I had to use echo -e $OUT to make the tabs and newlines work correctly.

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
}
@crsiebler

This comment has been minimized.

Copy link

@crsiebler crsiebler commented Mar 24, 2021

This article has a few handy ones. https://medium.com/hackernoon/handy-docker-aliases-4bd85089a3b8
Would you like me to add some of these to your file?

@jgrodziski

This comment has been minimized.

Copy link
Owner Author

@jgrodziski jgrodziski commented Mar 25, 2021

@crsibler yes sure feel free to add some of these aliases! thank you

@crsiebler

This comment has been minimized.

Copy link

@crsiebler crsiebler commented Mar 25, 2021

@jgrodziski here is my revision. https://gist.github.com/crsiebler/dff474dcf8f57f2ce59c2b2815892530/revisions This is my first time working with Gists. Is there no pull request type feature for them?

@mi6th

This comment has been minimized.

Copy link

@mi6th mi6th commented Mar 28, 2021

@crsiebler Correct, gists have no PR. Consider gists as code snippets or notes that can be e.g. discussed.

@arthuraldridge

This comment has been minimized.

Copy link

@arthuraldridge arthuraldridge commented Jul 12, 2021

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

This comment has been minimized.

Copy link

@iolpltuisciaoln iolpltuisciaoln commented Jul 12, 2021

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

This comment has been minimized.

Copy link

@mspronk 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

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