简体   繁体   English

如何在 Mac OS X 上为 Docker 添加 bash 命令完成?

[英]How to add bash command completion for Docker on Mac OS X?

我正在运行 docker,我想要bash命令完成docker命令和参数。

If you have already homebrew bash-completion installed just install the docker completion script into the bash_completion.d如果您已经自制bash-completion刚安装完成泊坞窗脚本到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.注意:如果您没有安装 homebrew bash-completion ,请在执行上面的行之前按照这些说明安装它。

Note: the completion depends on some functions defined in debian bash-completion .注意:完成取决于debian bash-completion 中定义的一些函数。 Therefore, just sourcing the docker completion script as described in completion/bash/docker may not work.因此,仅按照completion/bash/docker 中的描述获取 docker 完成脚本可能不起作用。 If you try to complete docker run (by hitting TAB) you may get an error like __ltrim_colon_completions: command not found .如果您尝试完成docker run (通过点击 TAB),您可能会收到类似__ltrim_colon_completions: command not found的错误。 This could mean that you have not installed the bash-completion scripts.这可能意味着您尚未安装 bash 完成脚本。

The official Docker for Mac page has a section on installing bash completion:官方 Docker for Mac 页面有一个关于安装 bash 完成的部分:

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

If you have Homebrew bash completion installed:如果您安装了 Homebrew bash 完成:

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

The completion scripts come with Docker Beta.完成脚本随 Docker Beta 一起提供。 I want them to stay up to date.我希望他们保持最新状态。 So, on OS X...所以,在 OS X 上...

  • Install homebrew's bash-completion安装 homebrew 的 bash-completion
  • Symlink the files符号链接文件

    find /Applications/Docker.app \\ -type f -name "*.bash-completion" \\ -exec ln -s "{}" "$(brew --prefix)/etc/bash_completion.d/" \\;

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:我的绊脚石是,一旦你 brew install bash-competion,你必须在你的 .bash_profile 中添加几行来在你启动终端后加载它:

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/资料来源: 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.因为我没有找到任何一步一步的文档,所以我制作了一个快速脚本来安装自制软件、bash 完成以及最终的 docker 完成脚本。 https://github.com/HypnoTheNomad/docker-bash-completion-macos-brew https://github.com/HypnoTheNomad/docker-bash-completion-macos-brew

The auto completion of docker needed not only for mac, its also needed for ubuntu / bash terminals. docker 的自动补全不仅需要mac,ubuntu/bash 终端也需要。

In Ubuntu在 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.由于它在谷歌中的最高结果,我在这里添加了答案。

The official Docker site has a section for Command-line completion, and for Mac to:官方 Docker 站点有一个用于命令行完成的部分,对于 Mac 来说:

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

Helped for me with Homebrew: Homebrew 对我有帮助:

brew install bash-completion

After the installation, Brew displays the installation path.安装完成后,Brew 会显示安装路径。 From documentation is correct for me:从文档对我来说是正确的:

/usr/local/etc/bash_completion.d/ /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 :打开并在文件末尾添加以下内容~/.bash_profile

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

Restart terminal.重启终端。 It's all.这就是全部。

Just to be said: if you use brew : brew install docker will do all what you need.只是说:如果你使用brewbrew install docker会做你需要的一切。 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. ...它包括brew link docker ,它将完成安装到`brew --prefix`/etc/bash_completion.d/docker相同的主题,相同的答案`brew --prefix`/etc/bash_completion.d/docker docker-machinedocker-compose等......

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否则(也许你正在使用 Docker Beta(没有 Virtualbox 的新的“更原生”的 docker 安装包)你仍然需要手动添加它,然后按照Michael 的回答加上查看 docker-machine、docker-compose 和的附加完成脚本在CodeCorrupt“脚本”中处理的一些 shell 助手

Guide to setup autocomplete for ZSH on Mac OSX在 Mac OSX 上为 ZSH 设置自动完成的指南

Follow these steps if you are using oh-my-zsh and autocomplete is not working :如果您使用的是oh-my-zsh并且自动完成功能不起作用,请按照以下步骤操作:

Step 1:第1步:

Make the following three links:制作以下三个链接:

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:第2步:

Either add autoload -Uz compinit; compinit要么添加autoload -Uz compinit; compinit autoload -Uz compinit; compinit to .zshrc autoload -Uz compinit; compinit.zshrc

or run echo "autoload -Uz compinit; compinit" >> .zshrc in your shell或者在你的 shell 中运行echo "autoload -Uz compinit; compinit" >> .zshrc

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM