(map blog thoughts)

Feb 7, 2018 - devops

Docker Tip #1 — Docker Aliases

I use docker daily, and having to type the same commands over and over again eventually gets old. Over the last few years I have written out, and stolen a few function and aliases that aid my daily workflow. In this post I will highlight a few of those, and hopefully you will find a way to incorporate these, or some variation of the same in yours.


There are several blog posts such as this one that got me started improving my daily workflow with Docker (Thanks James!). However, Docker 1.13 introduced a slew of changes, including restructuring the CLI to be easier to work with, and some of the posts out there, while useful, just happen to use an older API. I have tried to keep up my aliases and functions up-to-date with Docker’s CLI changes, so while you will see some overlapping functionality with some other posts out there, their implementations might be a tad newer.

Cleaning up

This is probably my most used Docker alias (at least per my bash history), and this is one of those gems I found in James' post, but I have tweaked to use the newer CLI API. Here goes

# drm stands for "docker remove"
alias drm="docker container prune -f"

This uses the new docker container API to remove all stopped containers (the -f simply does not wait for confirmation).

James also introduces a dri function, however, his removes any image that is not currently being used by a running container. I have rarely found the need to do that. I have, however, found the need to delete dangling images every so often. Here is my version of dri

# dri stands for "docker remove image"
alias dri="docker image prune -f"

The nice thing here is that if you did want to remove all images you can simply invoke dri with the -a flag. Note that you will not get a prompt asking if you are sure, and like I have said before, I have never felt the need to ever remove all images on my machine.

Running containers

The first alias here, I have blatantly stolen from James (Seriously, if you have not already, go read his post), but I will post the same here for posterity.

# dki stands for "docker interactive"
alias dki="docker run -itP"

Note that here I am supplying the -P flag, which means if you were to run an image that has a few ports EXPOSE-ed, Docker will automatically publish all of those ports on your host interface - don’t say I did not warn you :)

Using the same naming pattern, and on my continued usage of Docker, I came up with an alias of my own which allows me to quickly hop into a running container. Here is the alias itself:

# dke stands for "docker exec"
alias dke="docker exec -it"

Let us say I am running an Ubuntu container named basher, and I want to create an interactive bash session in the container — I can use this alias like so

# start a container
> docker run --name basher --rm -it ubuntu bash

# in another terminal
# use my alias to create a new bash session in the container
> dke basher bash



One last one. This function was the result of my hunting around StackOverflow along with some bash tricks I have picked up along the way. This alias helps me list out all the tags for a particular image in Docker Hub. This is particular useful when I am demoing something for an audience, or writing out a Dockerfile and would like to pin down the exact version of a particular image. Here goes:

dtags() {
  local image="${1}"
  curl -s -S "https://registry.hub.docker.com/v2/repositories/library/${image}/tags/" \
    | jq '."results"[]["name"]' \
    | sort

If you intend on using this one, you will need jq installed on your machine and available on your path.

To use it, simply invoke dtags with the name of an image that you would find in Docker Hub, like so

> dtags jenkins

And there you have it! A bunch of useful aliases and functions that you can adopt to your daily docker workflow.


There are no obvious downsides to using these aliases, with the exception of the downsides that come with using aliases and functions themselves. The first one is that it is the alias that gets recorded in your history, not the expanded version of the command, which means that if you are like me who routinely analyzes their CLI history, be prepared for a slightly skewed result set. This is particularly annoying if you change the definition of of an alias or a function — well, now you are doing two different things, but your history reports it as the same.

Second, using aliases means it is almost impossible, and incredibly frustrating working on any other machine than the one you have your aliases set onto! I constantly attempt to use my aliases on our Jenkins or development servers only to have the terminal remind me that they do not exist there.


Using aliases and functions can certainly make working with docker a lot easier, faster and less error prone. While using a post such as this one serves as a great starting point, it will serve you well in the long run to go back over your history, and discern patterns that you extract out into aliases and functions. A simple history | grep docker is a great start.

Good luck, and happy dockering!!