kubectl bash completion doesn't work in ubuntu docker container kubectl bash completion doesn't work in ubuntu docker container kubernetes kubernetes

kubectl bash completion doesn't work in ubuntu docker container


Ok, I found it - I simply needed to do a source /etc/bash_completion before or after the source <(kubectl completion bash).


A Linux container executed on macOS creates a separate environment andyes, it looks like a thread from macOS shell, but it is not. Shell history,properties, functions are a different story.Moreover, if the container has no persistent volume mounted all of those parameters will be transisten and won’t survive container’s restart.

The approach to have bash completion of both of them - macOS and UbuntuLinux are similar, but require different steps to take:

macOS side - permanent support for kubectl bash completion:

use homebrew to install support:

brew install bash-completionkubectl completion bash > $(brew --prefix)/etc/bash_completion.d/kubectl

Ubuntu container’s approach to have kubectl and bash completion support build in:

You can adapt this set of commands and use it in Dockerfile during the image preparation:

apt-get update && apt-get install -y apt-transport-httpscurl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | apt-key add -cat <<EOF >/etc/apt/sources.list.d/kubernetes.listdeb http://apt.kubernetes.io/ kubernetes-xenial mainEOFapt-get updateapt-get install -y kubectlecho 'source <(kubectl completion bash)' >> ~/.bashrc

If afterwards you or user executes /bin/bash in running container then you should get completion working.

docker exec -it docker_image_id /bin/bash

this will start bash shell with the bash completion.


check .bashrc

enable programmable completion features (you don't need to enablethis, if it's already enabled in /etc/bash.bashrc and /etc/profilesources /etc/bash.bashrc).

if [ -f /etc/bash_completion ] && ! shopt -oq posix; then    . /etc/bash_completionfi