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

MySQL 8.0.15 download and installation detailed tutorial is a must for novices!

This article records the specific steps for downl...

HTML web page creation tutorial Use iframe tags carefully

Using iframes can easily call pages from other we...

Vue sample code for easily implementing virtual scrolling

Table of contents Preface Rolling principle accom...

How to optimize MySQL query speed

In the previous chapters, we introduced how to ch...

Keepalived+Nginx+Tomcat sample code to implement high-availability Web cluster

Keepalived+Nginx+Tomcat to achieve high availabil...

Detailed explanation of the process of zabbix monitoring sqlserver

Let's take a look at zabbix monitoring sqlser...

How to Find the Execution Time of a Command or Process in Linux

On Unix-like systems, you may know when a command...

Introduction to HTML page source code layout_Powernode Java Academy

Introduction to HTML page source code layout This...

Sample code for implementing mysql master-slave replication in docker

Table of contents 1. Overview 1. Principle 2. Imp...

A simple example of using Vue3 routing VueRouter4

routing vue-router4 keeps most of the API unchang...

Implementing custom radio and check box functions with pure CSS

1. Achieve the effect 2 Knowledge Points 2.1 <...

Summary of H5 wake-up APP implementation methods and points for attention

Table of contents Preface Jump to APP method URL ...

Let's talk about the size and length limits of various objects in MySQL

Table of contents Identifier length limit Length ...

How many times will multiple setStates in React be called?

Table of contents 1. Two setState, how many times...

Docker network principles and detailed analysis of custom networks

Docker virtualizes a bridge on the host machine. ...