Docker Command Line Completion

I remember the first time that I discovered command line completion for my shell way back in the early 90’s.  Up until then, you needed to remember the filename you wanted to use for any command and type the full name out in its entirety.  This could be a frustrating and error-prone process even if you were a good typist. From then on being able to hit TAB and get the right filename was just pure bliss!

Depending on your situation, command line completion for Docker client may already be working and you might not realise it!  Read on for details.

Mac OS X

Firstly you must install the Homebrew package manager, if you haven’t done this already on your Mac. Install the bash-completion package to install the pieces necessary to make completion work:

$ brew install bash-completion

For it to actually work you need to add a snippet of code to your local bash configuration.  Add the following text to the end of your $HOME/.bash_profile file:

if [ -f $(brew --prefix)/etc/bash_completion ]; then
    . $(brew --prefix)/etc/bash_completion
fi

You can test by typing a partial docker command and hitting the TAB key:

$ docker im<TAB>
images  import

Here the command line completion script is asking you to pick between two possibilities that start with “im”.  Command line completion doesn’t just work with filenames!

Debian/Ubuntu

If you use Debian or Ubuntu then you’re in luck and command line completion should already be present and working.  This is true for both the vendor packaging installed with “apt-get install docker-engine” or “apt-get install docker-ce”, as well as the distribution packaging installed with “apt-get install docker.io”.

Fedora/CentOS/SuSE

If you use a RPM-based distribution like Fedora, CentOS or SuSE then you’re in luck also and command line completion should already be present and working.  Like for Debian and Ubuntu this should be true for both the vendor and distribution packaging.