56

I am running docker and I want bash command completion for docker commands and parameters.

8 Answers 8

75

If you have already homebrew bash-completion installed just install the docker completion script into the bash_completion.d

curl -XGET https://raw.githubusercontent.com/docker/cli/master/contrib/completion/bash/docker > $(brew --prefix)/etc/bash_completion.d/docker

Note: If you do not have homebrew bash-completion installed, follow these instructions to install it before you execute the line above.

Note: the completion depends on some functions defined in debian bash-completion. Therefore, just sourcing the docker completion script as described in completion/bash/docker may not work. If you try to complete docker run (by hitting TAB) you may get an error like __ltrim_colon_completions: command not found. This could mean that you have not installed the bash-completion scripts.

6
  • I have installed bash-completion and placed the docker completion script in the appropriate directory. But still the command completion is not working (I am not getting any error). Do I need to install bash too via homebrew? Or would it work with the bash included with Mavericks?
    – Joyce Babu
    Feb 4, 2015 at 10:29
  • Sorry, the docker completion file was empty since the curl command failed due to a certificate error. It is working now.
    – Joyce Babu
    Feb 4, 2015 at 10:37
  • 2
    The curl command works if you use -XGET instead of -GET: curl -XGET https://raw.githubusercontent.com/docker/docker/master/contrib/completion/bash/docker > `brew --prefix`/etc/bash_completion.d/docker Jun 26, 2015 at 7:06
  • 2
    I cannot get it working. I am using a Macbook and I have installed Docker using a standard .pkg file and not via brew. What am I missing?
    – Ahmadov
    Jul 25, 2016 at 8:52
  • Please look at @Hypno comment, that fixed it for me
    – yname
    Apr 11, 2018 at 18:39
39

The official Docker for Mac page has a section on installing bash completion:

https://docs.docker.com/docker-for-mac/#bash

If you have Homebrew bash completion installed:

cd /usr/local/etc/bash_completion.d
ln -s /Applications/Docker.app/Contents/Resources/etc/docker.bash-completion
ln -s /Applications/Docker.app/Contents/Resources/etc/docker-machine.bash-completion
ln -s /Applications/Docker.app/Contents/Resources/etc/docker-compose.bash-completion
1
  • For those using MacPorts, you'll need to sudo -s; port install bash-completion; cd /opt/local/etc/bash_completion.d, then go from there. You may need to set your login shell to /opt/local/bin/bash with chsh (see man chsh, or the discussion here). Both Terminal.app and iTerm also allow you to set a non-default shell, if you're apprehensive about changing it permanently.
    – Kevin E
    Nov 25, 2019 at 20:43
16

The completion scripts come with Docker Beta. I want them to stay up to date. So, on OS X...

  • Install homebrew's bash-completion
  • Symlink the files

    find /Applications/Docker.app \
    -type f -name "*.bash-completion" \
    -exec ln -s "{}" "$(brew --prefix)/etc/bash_completion.d/" \;
    
2
  • 3
    With the advent of the new native Docker on macOS, this should be the new top answer. Thanks!
    – Damon
    Nov 17, 2016 at 23:28
  • This will be extra handy if/when they add completion for kubectl to the list.
    – dragon788
    Mar 27, 2019 at 21:23
14

The stumbling point for me was that once you brew install bash-competion, you have to add few lines in your .bash_profile to load it once you launch Terminal:

if [ -f $(brew --prefix)/etc/bash_completion ]; then
. $(brew --prefix)/etc/bash_completion
fi

Source: http://davidalger.com/development/bash-completion-on-os-x-with-brew/

Because I haven't found anywhere a step by step documentation, I've made a quick script to install homebrew, bash-completion and eventually the completion scripts for docker. https://github.com/HypnoTheNomad/docker-bash-completion-macos-brew

1
  • 1
    @Hypno link is broken Jul 13, 2019 at 8:02
7

The auto completion of docker needed not only for mac, its also needed for ubuntu / bash terminals.

In Ubuntu

curl -ksSL https://raw.githubusercontent.com/docker/cli/master/contrib/completion/bash/docker |sudo tee /etc/bash_completion.d/docker

Completion will be available upon next login.

Since its top result in google I added answer here.

0
5

Guide to setup autocomplete for ZSH on Mac OSX

Follow these steps if you are using oh-my-zsh and autocomplete is not working:

Step 1:

Make the following three links:

on Apple Silicon Mac the target symlinks should go in /opt/homebrew, i.e. replace /usr/local in above paths with /opt/homebrew

ln -s /Applications/Docker.app/Contents/Resources/etc/docker.zsh-completion /usr/local/share/zsh/site-functions/_docker

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

Step 2:

Either add autoload -Uz compinit; compinit to .zshrc

or run echo "autoload -Uz compinit; compinit" >> .zshrc in your shell

Step 3:

Apply the change to your terminal:

source .zshrc

1
  • 1
    If on Apple Silicon Mac the target symlinks should go in /opt/homebrew, i.e. replace /usr/local in above paths with /opt/homebrew Mar 21, 2023 at 15:54
2

The official Docker site has a section for Command-line completion, and for Mac to:

https://docs.docker.com/compose/completion/#mac

Helped for me with Homebrew:

brew install bash-completion

After the installation, Brew displays the installation path. From documentation is correct for me:

/usr/local/etc/bash_completion.d/

Run the script:

sudo curl -L https://raw.githubusercontent.com/docker/compose/1.25.4/contrib/completion/bash/docker-compose -o /usr/local/etc/bash_completion.d/docker-compose

Open and add following in the end of your file ~/.bash_profile:

if [ -f $(brew --prefix)/etc/bash_completion ]; then
 . $(brew --prefix)/etc/bash_completion
 fi

Restart terminal. It's all.

1

Just to be said: if you use brew: brew install docker will do all what you need. It includes brew link docker which installs the completion into `brew --prefix`/etc/bash_completion.d/docker same topic, same answer for docker-machine, docker-compose, etc. ...

else (perhaps you are using Docker Beta (new "more native" docker installation package without Virtualbox) you still have to add it manually, then follow Michael's answer plus have a look at the additional completion scripts for docker-machine, docker-compose and some shell-helper that are handled in the 'script' from CodeCorrupt

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.