Enable Docker command-line auto completion in bash on Centos/Ubuntu

UPDATE:

Docker comes with command completion for the bash and zsh shell.
Here is the procedure to enable bash auto completion in Docker CE on Centos 7 and Ubuntu

Install bash-completion package

on CentOS/RedHat

# yum -y install bash-completion

on Ubuntu

apt-get update
apt-get install bash-completion -y

UPDATE: June 27, 2020

If you installed docker-ce-cli package, it already ships with bash-completion files, you don't need to run the following commands.# dpkg -L docker-ce-cli |grep completion
/usr/share/bash-completion
/usr/share/bash-completion/completions
/usr/share/bash-completion/completions/docker
/usr/share/fish/vendor_completions.d
/usr/share/fish/vendor_completions.d/docker.fish
/usr/share/zsh/vendor-completions
/usr/share/zsh/vendor-completions/_docker

Download bash completion file from https://github.com/docker/docker-ce/blob/master/components/cli/contrib/completion/bash/docker into /etc/bash_completion.d

# curl https://raw.githubusercontent.com/docker/docker-ce/master/components/cli/contrib/completion/bash/docker -o /etc/bash_completion.d/docker.sh

Logout and login again.

type docker im and enter TAB then, you will get the possible parameters

# docker im<TAB>
image images import

If I type docker images then TAB, docker will list the available images.

# docker images  <TAB>
nginx:latest redis

It alo shows us the parameter of each command

# docker ps -<TAB>
-a -f --format -l --latest --no-trunc --quiet --size
--all --filter --help --last -n -q -s

İsmail Yenigül

Linux & AWS Certified Cloud Expert