kubectl bash completion doesn't work in ubuntu docker container
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
kubectl bash completion works in a Docker container only if the shell environment is set up like a real interactive Bash session. In minimal Ubuntu images, one or more pieces are usually missing: the bash-completion package, a Bash shell, the completion script, or the startup file that sources it. The fix is usually straightforward once you verify those pieces in order.
What kubectl Completion Needs
For tab completion to work, all of these must be true:
- you are actually running
bash, notsh - the
bash-completionpackage is installed - '
kubectl completion bashis sourced into the shell' - the shell is interactive
If any of those are missing, pressing Tab will do nothing.
Start with the generated completion script itself:
If that command prints a Bash completion script, kubectl itself supports completion and the problem is shell setup, not the kubectl binary.
A Working Ubuntu Container Setup
A minimal Dockerfile for interactive use looks like this:
The important details are:
- install
bash-completion - source the system completion file
- source the
kubectlcompletion output - start an interactive Bash shell
Why Containers Often Break This
Many Docker workflows use commands like these:
That usually works if .bashrc exists and Bash is interactive.
But completion often fails in these situations:
- the image starts with
shinstead ofbash - the shell is non-interactive, so
.bashrcis not sourced - '
.bashrcaddskubectlcompletion but never sourcesbash-completion' - the container image is so minimal that completion infrastructure is missing entirely
An easy sanity check inside the container is:
If the shell is not Bash, or if the shell flags do not include i, or if _init_completion is missing, the completion environment is incomplete.
A Manual Fix Inside a Running Container
If the image already exists, you can test completion manually before changing the Dockerfile:
If complete | grep kubectl shows a completion definition, the setup is correct for that shell session.
To make it persistent, append the source commands to /root/.bashrc or the relevant user's shell profile.
The Docker Exec Detail
Another common trap is docker exec.
This gives you an interactive shell, but it only picks up completion if the shell startup files are configured in the container. If you use docker exec my-container kubectl get pods, there is no interactive Bash session, so completion is irrelevant.
Common Pitfalls
The biggest mistake is trying to use completion in a shell that is actually dash or sh. kubectl completion bash only helps Bash.
Another mistake is sourcing kubectl completion bash without first loading the system bash-completion package.
A third issue is testing in a non-interactive container command and expecting .bashrc to load automatically.
Summary
- '
kubectlcompletion in Docker requires Bash,bash-completion, and an interactive shell' - Verify the generated script with
kubectl completion bash | head - Source both
/usr/share/bash-completion/bash_completionandsource <(kubectl completion bash) - Persist the setup in
.bashrcif you want it every time the container starts - If completion still fails, check whether you are actually in interactive Bash rather than
shor a non-interactive shell

