kubectl
bash completion
Ubuntu
Docker
troubleshooting

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, not sh
  • the bash-completion package is installed
  • 'kubectl completion bash is 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:

bash
kubectl completion bash | head

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:

dockerfile
1FROM ubuntu:24.04
2
3RUN apt-get update && apt-get install -y \
4    bash \
5    bash-completion \
6    curl \
7    ca-certificates \
8    && rm -rf /var/lib/apt/lists/*
9
10RUN curl -LO "https://dl.k8s.io/release/v1.34.0/bin/linux/amd64/kubectl" \
11    && install -m 0755 kubectl /usr/local/bin/kubectl \
12    && rm kubectl
13
14RUN echo 'source /usr/share/bash-completion/bash_completion' >> /root/.bashrc \
15    && echo 'source <(kubectl completion bash)' >> /root/.bashrc
16
17CMD ["bash", "-i"]

The important details are:

  • install bash-completion
  • source the system completion file
  • source the kubectl completion output
  • start an interactive Bash shell

Why Containers Often Break This

Many Docker workflows use commands like these:

bash
docker run --rm -it my-image bash

That usually works if .bashrc exists and Bash is interactive.

But completion often fails in these situations:

  • the image starts with sh instead of bash
  • the shell is non-interactive, so .bashrc is not sourced
  • '.bashrc adds kubectl completion but never sources bash-completion'
  • the container image is so minimal that completion infrastructure is missing entirely

An easy sanity check inside the container is:

bash
echo "$0"
echo "$-"
type _init_completion

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:

bash
1apt-get update && apt-get install -y bash-completion
2source /usr/share/bash-completion/bash_completion
3source <(kubectl completion bash)
4complete | grep kubectl

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.

bash
docker exec -it my-container bash

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

  • 'kubectl completion 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_completion and source <(kubectl completion bash)
  • Persist the setup in .bashrc if you want it every time the container starts
  • If completion still fails, check whether you are actually in interactive Bash rather than sh or a non-interactive shell

Course illustration
Course illustration

All Rights Reserved.