How to add bash command completion for Docker on M

2019-03-10 16:30发布

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

标签: macos docker
6条回答
一夜七次
2楼-- · 2019-03-10 16:58

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/docker/$(docker --version | awk 'NR==1{print $NF}')/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.

查看更多
ら.Afraid
3楼-- · 2019-03-10 17:04

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.

查看更多
你好瞎i
4楼-- · 2019-03-10 17:06

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/" \;
    
查看更多
放荡不羁爱自由
5楼-- · 2019-03-10 17:16

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

查看更多
\"骚年 ilove
6楼-- · 2019-03-10 17:19

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

查看更多
我命由我不由天
7楼-- · 2019-03-10 17:21

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
查看更多
登录 后发表回答