Fixing Docker Command Auto Completion in Mac OS Catalina

After having upgraded to Mac OS Catalina the configuration of docker auto completion for both bash and zsh as suggested here will no longer work - probably due to the read-only system volume.

The Problem

For zsh e.g. the suggested

# directories adapted to Catalina's default zsh installation, not e.g. via homebrew which is /usr/local/share/zsh/site-functions/

etc=/Applications/Docker.app/Contents/Resources/etc
ln -s $etc/docker.zsh-completion /usr/share/zsh/site-functions/_docker
ln -s $etc/docker-machine.zsh-completion /usr/share/zsh/site-functions/_docker-machine
ln -s $etc/docker-compose.zsh-completion /usr/share/zsh/site-functions/_docker-compose

will yield the following errors

ln: /usr/share/zsh/site-functions/_docker: Operation not permitted
ln: /usr/share/zsh/site-functions/_docker-machine: Operation not permitted
ln: /usr/share/zsh/site-functions/_docker-compose: Operation not permitted

not matter which user privileges.

One Fix

But, at least for zsh, there is another option which does not require writing/linking to the system volume. It rather uses the mac os user directory and then tells zsh to look there for docker auto completion.

First it needs the following directories and symlinks,

# again: directories adapted to Catalina's default zsh installation, not e.g. via homebrew which is /usr/local/share/zsh/site-functions/

mkdir -p ~/.zsh/completion
ln -s /Applications/Docker.app/Contents/Resources/etc/docker.zsh-completion ~/.zsh/completion/_docker
ln -s /Applications/Docker.app/Contents/Resources/etc/docker-machine.zsh-completion ~/.zsh/completion/_docker-machine
ln -s /Applications/Docker.app/Contents/Resources/etc/docker-compose.zsh-completion ~/.zsh/completion/_docker-compose

and then the following entries in your ~/.zshrc

autoload -Uz compinit
fpath=(~/.zsh/completion $fpath)
compinit -i

After that the docker auto completion for zsh is available in every new shell or after executing . ~/.zshrc again in the current shell.