kubectl bash completion doesn't work in ubuntu docker container
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
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
Related reading
- kubectl command to get list of pods running on k8s master node
- Kubectl command to list pods of a deployment in Kubernetes
- Kubectl command to return a list of all user accounts from Kubernetes
- kubectl context vs cluster
- kubectl exec into a container without using the random guid at the end of the pod name
- kubectl exec into container of a multi container pod
- kubectl cp error one of src or dest must be a remote file specification
- Kubectl create for persistent storage erroring out

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.