2

Useful Docker Aliases

 1 year ago
source link: https://gist.github.com/jgrodziski/9ed4a17709baad10dbcd4530b60dfcbb
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Author

jgrodziski commented on Nov 3, 2016

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

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

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

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

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

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

Author

jgrodziski commented on 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

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

noplanman commented on Mar 18, 2021

edited

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
}

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?

Author

jgrodziski commented on Mar 25, 2021

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

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

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?

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 commented on Aug 21, 2021

edited

@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

How can I use this aliases with zsh?

ZahirJ commented on Mar 6

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"

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

danielTobon43 commented on May 6

edited

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.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK