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

Thanks a lot for your help 👍 😄

@BretFisher
Copy link

Assuming you're using "Docker for Mac" from store.docker.com, it's idea to use the bash completion files that come with it. The Docker for Mac client keeps itself up to date and comes with the latest completion changes, which are often. Also, the Docker for Mac method includes docker-machine's completion too. Read more at their docs site or just see the below:

# replace the curl commands above with the following, after you've installed Docker for Mac and the brew bash completion steps
ln -s /Applications/Docker.app/Contents/Resources/etc/docker.bash-completion /usr/local/etc/bash_completion.d/docker
ln -s /Applications/Docker.app/Contents/Resources/etc/docker-machine.bash-completion /usr/local/etc/bash_completion.d/docker-machine
ln -s /Applications/Docker.app/Contents/Resources/etc/docker-compose.bash-completion /usr/local/etc/bash_completion.d/docker-compose

@ahsansher
Copy link

ahsansher commented Feb 24, 2018

@BretFisher you are a life saver 👍

@gcstang
Copy link

gcstang commented Mar 7, 2018

@BretFisher worked perfectly!

Thank you!

@johanvanderkuijl
Copy link

@BretFisher thank you! I just converted from win/linux to mac and this was what I was looking for.

@Itsindigo
Copy link

Itsindigo commented May 22, 2018

Thank you @BretFisher

Worth noting that if you've been following the advice of other issues, or you ran the commands at the top of this post before seeing Bret's answer then you should delete you docker/docker-machine/docker-compose files in bash_completion.d, as the sym links that Bret suggests will fail silently otherwise

@dewijones92
Copy link

@BretFisher thanks!!!!

@cmoro-deusto
Copy link

@BretFisher, thanks a lot! Curious: I wonder why this is not done automatically by the Docker installer on mac, any clue?

@toxakktl
Copy link

@BretFisher, Hey, I updated to OS Mojave and it does not work :(

@mohsenasm
Copy link

@BretFisher, Thank you
@toxakktl, It works for me on Mojave:

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

Read more at their docs site

@kmaqsudi
Copy link

kmaqsudi commented Dec 9, 2018

Thank you @BretFisher

@castasamu
Copy link

Thanks @BretFisher @mohsenasm. it works on my mojave!!

@xCASx
Copy link

xCASx commented Jan 5, 2019

@BretFisher thank you, works like a charm on my Mojave!

@toxakktl keep in mind that you still need to add this to your ~/.bash_profile:

[ -f /usr/local/etc/bash_completion ] && . /usr/local/etc/bash_completion

@darkdreamingdan
Copy link

darkdreamingdan commented Jan 18, 2019

The second curl link now fails, it's a broken link and you'll end up downloading a 404. Looks like the repo has been renamed cli:

curl -L https://raw.githubusercontent.com/docker/cli/master/contrib/completion/bash/docker > /usr/local/etc/bash_completion.d/docker

Please update your gist! :)

@krearthur
Copy link

Thanks a lot!

@bcavarun
Copy link

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

Worked for me.

@dragosMC91
Copy link

Thank you ! :D

@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