Skip to content

Instantly share code, notes, and snippets.

@rkuzsma
Last active November 18, 2023 09:11
Show Gist options
  • Save rkuzsma/4f8c1354a9ea67fb3ca915b50e131d1c to your computer and use it in GitHub Desktop.
Save rkuzsma/4f8c1354a9ea67fb3ca915b50e131d1c to your computer and use it in GitHub Desktop.
How to configure Bash Completion on Mac for Docker and Docker-Compose

How to configure Bash Completion on Mac for Docker and Docker-Compose

Copied from the official Docker-for-mac documentation (thanks Brett for the updated doc pointer):

Install shell completion

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

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

Zsh

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
@RPing
Copy link

RPing commented Sep 13, 2022

you save my life.

@massisenergy
Copy link

👍🏽

@pravic
Copy link

pravic commented Apr 3, 2023

For some reason, with these completions each bash session starts, like, for 4-6 seconds, which is nonsense. It looks as if bash-completion scripts are running several docker binaries and trying to parse their output.

To avoid this, there's an ugly workaround to defer that initialization until it is needed:

function dockerenv() {
  src=/Applications/Docker.app/Contents/Resources/etc
  dst=/usr/local/etc/bash_completion.d

  ln -s $src/docker.bash-completion $dst/docker
  ln -s $src/docker-compose.bash-completion $dst/docker-compose

  source /usr/local/etc/bash_completion

  rm $dst/docker
  rm $dst/docker-compose
  echo "Docker completion works."
}

Docker Desktop version 4.17, Engine 20.10.
macOS 13.2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment