Score:1

How to fix docker container autocompletion

pt flag

I have installed docker on many machines in the past and auto-completion always had just worked out of the box. Strangely, in a new docker install on an Ubuntu 20.04.2 LTS (Focal Fossa) virtual machine, when I press tab, nothing happens. I tried it on my WSL terminal, and if I type sudo docker start g and then tab, it autocompletes to sudo docker start gifted_hofstadter. Yet, both only have these two files in:

# ls /etc/bash_completion.d/
apport_completion  git-prompt

So I don't understand why I would have to add a /etc/bash_completion.d/docker file as an article suggests. How do I fix it with minimal changes? To be clear, regular bash autocompletion works fine, the issue is specific to docker. Thanks.

Update: Installed podman and the same autocomplete issue exists although I don't think it's related.

I copied this docker.sh file to /etc/bash_completion.d/ and restarted the shell. Now docker commands autocomplete, e.g. docker sta [tab] expands to docker start. Also if I do docker start f it expands to docker start f36da5e5a9a8 i.e. the container ID.

However, it won't expand the container name, such as frosty_tu when I do docker start fro [tab]. While I appreciate the solution, this is another reason I don't want to shop around for third party scripts and prefer the out-of-the-box experience I have seen on new installs before.

David avatar
cn flag
There is no version of Ubuntu 20 LTS
Nagev avatar
pt flag
That's correct. Was writing from memory in a rush and thought that the approximate, i.e. major (YY) version, was enough. Updated with the full version, thanks.
Artur Meinild avatar
vn flag
Completion scripts are located under `/usr/share/bash-completion/completions`. You should state how you installed Docker, there are at least 3 different options: 1) Snap, 2) Default APT, and 3) APT from Docker repos. I've installed Docker using method 3 (Docker repos), and my autocompletion works with container names, but not with container ID.
Nagev avatar
pt flag
Funny coincidence, I had actually just thought of comparing docker's snap vs the one from the default APT, i.e. 1) and 2) from your list, after running twice on a [docker cp](https://github.com/docker/for-linux/issues/564) snap bug. I see this issue on two servers with snap's docker. Not sure what my previous WSL (which had good docker autocompletion) had as I have a new one with Ubuntu 22.04.1 LTS without docker yet. Will try 1) and 2) there and update accordingly. By the way, it is the container name autocompletion I care about.
Nagev avatar
pt flag
It seems that APT no longer has even an ancient version of docker and the [docker install documentation](https://docs.docker.com/engine/install/ubuntu/) reminded me of why I went with the convenience of `snap`. And I forgot that *snap* isn't available on WSL. Anyway, I'll probably try switching at some point, but for now, I need my containers already created with snap, and not too keen on making a switch without being sure it will solve the issue. Updated the question to mention that I am using snap, thanks.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.