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

cool, worked, thanks!

macOS Mojave (10.14.6)
docker version: 19.03.8

@jllg18
Copy link

jllg18 commented Oct 17, 2021

Run . /usr/local/etc/bash_completion to enable the bash completion after steps and the file to modified in Big SUR OS are bashrc or zshrc in /etc

@ernstki
Copy link

ernstki commented Jan 7, 2022

Catalina, using docker, docker-machine, and docker-compose from MacPorts:

RAW=https://raw.githubusercontent.com/docker
cd /opt/local/etc/bash_completion.d

curl -L $RAW/cli/master/contrib/completion/bash/docker \
  | sed '/^_docker_compose() {/,/^}/d' | sudo tee docker.bash >/dev/null
sudo curl -L $RAW/machine/master/contrib/completion/bash/docker-machine.bash -o docker-machine.bash
sudo curl -L $RAW/compose/1.29.2/contrib/completion/bash/docker-compose -o docker-compose.bash

The sed … | sudo tee rigamarole is because, as of this writing (7 January 2022), the docker/cli completion script contains a _docker_compose function that is probably intended for docker compose, but the version of docker in MacPorts doesn't have that, and it interferes with the same-named function in docker-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:

docker-compose.bash: Bourne-Again shell script text executable, ASCII text, with very long lines
docker-machine.bash: ASCII text
docker.bash:         Bourne-Again shell script text executable, ASCII text, with very long lines

Side note: MacPorts puts the bash-completion bootstrap script in /opt/local/etc/profile.d, so your ~/.bash_profile should contain something like this

[ -f /opt/local/etc/profile.d/bash_completion.sh ] &&
    . /opt/local/etc/profile.d/bash_completion.sh

…which the bash-completion port will remind you of when you first install it.

@notDavid
Copy link

notDavid commented Jul 2, 2022

"Docker for Mac" completions for the "fish shell" :

ln -s /Applications/Docker.app/Contents/Resources/etc/docker-compose.fish-completion ~/.config/fish/completions/docker-compose.fish
ln -s /Applications/Docker.app/Contents/Resources/etc/docker.fish-completion ~/.config/fish/completions/docker.fish

@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