Docker CLI Bash completion on MacOS

Docker Logo

Docker CLI tool has many commands. If you are also managing a large number of containers, images and networks you will find yourself going back and forth to remember all names, identifiers and parameters. To get some help in this task we can use bash-autocompletion so it does all the magic for us when we push the "tab" key.

First, install bash-completion

If you have homebrew installed it is really easy, just do the following:

brew install bash-completion

If you don't have brew, please follow the instructions in brew homepage.

Second, add docker completion

They are already installed with Docker for Mac. So, let's link them in:

/usr/local/etc/bash_completion.d

cd /usr/local/etc/bash_completion.d  
ln -s /Applications/Docker.app/Contents/Resources/etc/docker.bash-completion  
ln -s /Applications/Docker.app/Contents/Resources/etc/docker-machine.bash-completion  
ln -s /Applications/Docker.app/Contents/Resources/etc/docker-compose.bash-completion  

Third, configure your bash_profile

Add the following to your use's .bash_profile:

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

That piece of code will load all the profiles stored in the previous directory. That means that our "docker" auto-completion code will be enabled.

Fourth, and final step, test it

Just write docker container create and press tab twice to get the auto-completion. No need to remember or check the names of the images you have anymore :)

$ docker container create [tab tab]
alpine           debian           httpd            mysql            nginx:latest     ubuntu:14.04
alpine:latest    debian:latest    httpd:latest     mysql:5.6        percona          ubuntu:latest
centos           elasticsearch    mongo            mysql:latest     percona:latest   ubuntu:trusty
centos:7         elasticsearch:2  mongo:latest     nginx            ubuntu