当前位置: 首页 > 工具软件 > docker-client > 使用案例 >

docker-compose命令补全

澹台奇略
2023-12-01

docker-compose可以使用bash-completion进行命令补全,这篇文章以docker 1.21.1为例进行说明如何设定。

Bash命令补全

kubectl命令补全实际是使用bash-completion的功能,而关于bash-completion的说明,可参看如下内容:

  • https://liumiaocn.blog.csdn.net/article/details/103332169

macOS设定命令补全

事前准备

  • docker 环境准备
liumiaocn:~ liumiao$ docker version
Client:
 Version:      18.03.1-ce
 API version:  1.37
 Go version:   go1.9.5
 Git commit:   9ee9f40
 Built:        Thu Apr 26 07:13:02 2018
 OS/Arch:      darwin/amd64
 Experimental: false
 Orchestrator: swarm

Server:
 Engine:
  Version:      18.03.1-ce
  API version:  1.37 (minimum version 1.12)
  Go version:   go1.9.5
  Git commit:   9ee9f40
  Built:        Thu Apr 26 07:22:38 2018
  OS/Arch:      linux/amd64
  Experimental: true
liumiaocn:~ liumiao$
  • docker-compose
liumiaocn:~ liumiao$ docker-compose version
docker-compose version 1.21.1, build 5a3f1a3
docker-py version: 3.3.0
CPython version: 3.6.4
OpenSSL version: OpenSSL 1.0.2o  27 Mar 2018
liumiaocn:~ liumiao$ 
  • bash-completion准备
liumiaocn:~ liumiao$ brew list bash-completion
/usr/local/Cellar/bash-completion/1.3_3/etc/bash_completion.d/ (180 files)
/usr/local/Cellar/bash-completion/1.3_3/etc/profile.d/bash_completion.sh
/usr/local/Cellar/bash-completion/1.3_3/etc/bash_completion
liumiaocn:~ liumiao$ 
  • 操作系统与当前SHELL变量
liumiaocn:~ liumiao$ sw_vers
ProductName:	Mac OS X
ProductVersion:	10.14
BuildVersion:	18A391
liumiaocn:~ liumiao$ uname -a
Darwin liumiaocn 18.0.0 Darwin Kernel Version 18.0.0: Wed Aug 22 20:13:40 PDT 2018; root:xnu-4903.201.2~1/RELEASE_X86_64 x86_64
liumiaocn:~ liumiao$ echo $SHELL
/bin/bash
liumiaocn:~ liumiao$ 

补全设定方法: docker-compose

  • 步骤1: 设定docker-compose的补全文件
liumiaocn:~ liumiao$ curl -L https://raw.githubusercontent.com/docker/compose/1.25.0/contrib/completion/bash/docker-compose -o docker-compose
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 13383  100 13383    0     0  29477      0 --:--:-- --:--:-- --:--:-- 29477
liumiaocn:~ liumiao$ wc -l docker-compose 
     678 docker-compose
liumiaocn:~ liumiao$ cp docker-compose /usr/local/etc/bash_completion.d/docker-compose
liumiaocn:~ liumiao$ ls /usr/local/etc/bash_completion.d/docker-compose
/usr/local/etc/bash_completion.d/docker-compose
liumiaocn:~ liumiao$ 
  • 步骤2: 设定.bash_profile
    添加如下内容至.bash_profile
if [ -f $(brew --prefix)/etc/bash_completion ]; then
. $(brew --prefix)/etc/bash_completion
fi

结果确认

liumiaocn:~ liumiao$ docker-compose s
scale  start  stop   
liumiaocn:~ liumiao$ docker-compose s

CentOS设定命令补全

事前准备

  • docker 环境准备
[root@host122 ~]# docker version
Client:
 Version:         1.13.1
 API version:     1.26
 Package version: docker-1.13.1-103.git7f2769b.el7.centos.x86_64
 Go version:      go1.10.3
 Git commit:      7f2769b/1.13.1
 Built:           Sun Sep 15 14:06:47 2019
 OS/Arch:         linux/amd64

Server:
 Version:         1.13.1
 API version:     1.26 (minimum version 1.12)
 Package version: docker-1.13.1-103.git7f2769b.el7.centos.x86_64
 Go version:      go1.10.3
 Git commit:      7f2769b/1.13.1
 Built:           Sun Sep 15 14:06:47 2019
 OS/Arch:         linux/amd64
 Experimental:    false
[root@host122 ~]# 
  • docker-compose
[root@host122 ~]# docker-compose version
docker-compose version 1.21.1, build 5a3f1a3
docker-py version: 3.3.0
CPython version: 3.6.5
OpenSSL version: OpenSSL 1.0.1t  3 May 2016
[root@host122 ~]# 
  • bash-completion准备
[root@host122 ~]# rpm -qa |grep bash-completion
bash-completion-2.1-6.el7.noarch
[root@host122 ~]#
  • 操作系统与当前SHELL变量
[root@host122 ~]# cat /etc/redhat-release 
CentOS Linux release 7.6.1810 (Core) 
[root@host122 ~]# uname -a
Linux host122 3.10.0-957.el7.x86_64 #1 SMP Thu Nov 8 23:39:32 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux
[root@host122 ~]# 
[root@host122 ~]# echo $SHELL
/bin/bash
[root@host122 ~]# 

补全设定方法: docker-compose

设定docker-compose的补全文件

[root@host122 ~]# curl -L https://raw.githubusercontent.com/docker/compose/1.25.0/contrib/completion/bash/docker-compose -o docker-compose
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 13383  100 13383    0     0  28475      0 --:--:-- --:--:-- --:--:-- 28474
[root@host122 ~]# ls docker-compose 
docker-compose
[root@host122 ~]# wc -l docker-compose 
678 docker-compose
[root@host122 ~]# mkdir -p /etc/bash_completion.d/
[root@host122 ~]# cp docker-compose /etc/bash_completion.d/docker-compose
[root@host122 ~]# 

注:如果网络状态不好,无法直接下载,可以通过浏览器获得,然后使用vi生成此文件也可以。

结果确认

[root@host122 ~]# docker-compose s
scale  start  stop   
[root@host122 ~]#

参考内容

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

 类似资料: