Implementation of automatic completion of Docker commands

Implementation of automatic completion of Docker commands

Preface

I don't know how long this friend hasn't used Docker, and suddenly asked me why the Docker command has changed

docker run ...
#Changed to docker container run ...

He said that he was not familiar with Docker commands, and now it felt even more confusing. In fact, in my opinion, this change makes the command look more regular.

When you directly enter docker in the command line and press Enter:

As can be seen from the figure, Docker divides the command structure into two categories, Management Commands and Commands. In fact, the former is a first-level command, and the latter is a sub-command (this is a change since Docker 1.13), so the command will be used like this in the future:

docker <Management Command> <Sub-Command <Opts/Args>>

In this way, when we use commands in the future, we only need to focus on Management Commands first. But if we still don’t know how to use the subsequent sub-commands, do we need to query them one by one?

Docker command autocompletion

To solve this problem, Docker also provides a very complete command auto-completion function, which means leaving everything to the Tab key.

Mac installation Docker command auto-completion

Type the following commands one by one:

brew install bash-completion

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

Open the ~/.bash_profile file and paste the following content into it:

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

Then refresh to make it effective

source ~/.bash_profile 

I think Zsh is better, why? For the answer, please see this article: This iTerm2 + Oh My Zsh tutorial will help you become the coolest guy on the street

Zsh installs Docker command autocompletion

If you don't have Oh-My-Zsh shell installed, the first step is to install it by typing the following commands one by one:

mkdir -p ~/.zsh/completion

curl -L https://raw.githubusercontent.com/docker/compose/1.27.4/contrib/completion/zsh/_docker-compose > ~/.zsh/completion/_docker-compose

Open the ~/.zshrc file and paste the following content into it:

fpath=(~/.zsh/completion $fpath)
autoload -Uz compinit && compinit -i

For example, the content of my ~/.zshrc file is:

Search for the file plugin location and update the plugin content:

plugins=(... docker docker-compose
) 

By the way, it is strongly recommended to use the git plugin

Finally refresh it to make it take effect:

source ~/.zshrc

The auto-completion function can make crazy use of your Tab key, which is faster than consulting the document. Let's take a look at the effect:


This is the end of this article about the implementation of Docker command auto-completion. For more related Docker command auto-completion content, please search 123WORDPRESS.COM’s previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Solution to the problem that commands cannot be completed during docker testing

<<:  Solution to mysql error code 1064

>>:  What are the differences between var let const in JavaScript

Recommend

Records of using ssh commands on Windows 8

1. Open the virtual machine and git bash window a...

This article will help you understand JavaScript variables and data types

Table of contents Preface: Kind tips: variable 1....

How to install redis in Docke

1. Search for redis image docker search redis 2. ...

What to do if you forget the initial password of MySQL on MAC

The method to solve the problem of forgetting the...

Two usages of iFrame tags in HTML

I have been working on a project recently - Budou...

JavaScript to achieve slow motion animation effect

This article shares the specific code for JavaScr...

Detailed explanation of the cache implementation principle of Vue computed

Table of contents Initialize computed Dependency ...

How to use Vuex's auxiliary functions

Table of contents mapState mapGetters mapMutation...

Detailed explanation of MySQL deadlock and database and table sharding issues

Record the problem points of MySQL production. Bu...

Linux file/directory permissions and ownership management

1. Overview of file permissions and ownership 1. ...

Should I use Bootstrap or jQuery Mobile for mobile web wap

Solving the problem Bootstrap is a CSS framework ...

Building command line applications with JavaScript

Table of contents 1. Install node 2. Install Comm...

JavaScript realizes the drag effect of modal box

Here is a case of modal box dragging. The functio...

Detailed explanation of explain type in MySQL

Introduction: In many cases, many people think th...