Copied from the official Docker-for-mac documentation (thanks Brett for the updated doc pointer):
Docker Desktop for Mac comes with scripts to enable completion for the docker, docker-machine, and docker-compose commands. The completion scripts may be found inside Docker.app, in the Contents/Resources/etc/ directory and can be installed both in Bash and Zsh.
Bash has built-in support for completion To activate completion for Docker commands, these files need to be copied or symlinked to your bash_completion.d/ directory. For example, if you installed bash via Homebrew:
etc=/Applications/Docker.app/Contents/Resources/etc
ln -s $etc/docker.bash-completion $(brew --prefix)/etc/bash_completion.d/docker
ln -s $etc/docker-machine.bash-completion $(brew --prefix)/etc/bash_completion.d/docker-machine
ln -s $etc/docker-compose.bash-completion $(brew --prefix)/etc/bash_completion.d/docker-compose
Add the following to your ~/.bash_profile:
[ -f /usr/local/etc/bash_completion ] && . /usr/local/etc/bash_completion
OR
if [ -f $(brew --prefix)/etc/bash_completion ]; then
. $(brew --prefix)/etc/bash_completion
fi
In Zsh, the completion system takes care of things. To activate completion for Docker commands, these files need to be copied or symlinked to your Zsh site-functions/ directory. For example, if you installed Zsh via Homebrew:
etc=/Applications/Docker.app/Contents/Resources/etc
ln -s $etc/docker.zsh-completion /usr/local/share/zsh/site-functions/_docker
ln -s $etc/docker-machine.zsh-completion /usr/local/share/zsh/site-functions/_docker-machine
ln -s $etc/docker-compose.zsh-completion /usr/local/share/zsh/site-functions/_docker-compose
Catalina, using
docker,docker-machine, anddocker-composefrom MacPorts:The
sed … | sudo teerigamarole is because, as of this writing (7 January 2022), thedocker/clicompletion script contains a_docker_composefunction that is probably intended fordocker compose, but the version ofdockerin MacPorts doesn't have that, and it interferes with the same-named function indocker-compose.bash. So I cut it out with sed.To make sure you actually got the files you intended without any 404s, it would be helpful to run
file /opt/local/etc/bash_completion.d/docker*, the output of which should look something like:Side note: MacPorts puts the bash-completion bootstrap script in
/opt/local/etc/profile.d, so your~/.bash_profileshould contain something like this…which the bash-completion port will remind you of when you first install it.