Search code examples
bashkuberneteskubectl

Why are shell builtins not found when using Kubectl exec


I am making a bash script to copy files from a Kubernetes pod running Debian. When I include the following line:

kubectl --namespace "$namesp" exec "$pod" -c "$container" -- cd /var

it errors out:

OCI runtime exec failed: exec failed: container_linux.go:380: starting container process caused: exec: "cd": executable file not found in $PATH: unknown
command terminated with exit code 126

I also tried

kubectl --namespace "$namesp" exec "$pod" -c "$container" -- builtin
kubectl --namespace "$namesp" exec "$pod" -c "$container" -it -- cd /var

which gave the same result.

I was able to resolve the issue by changing the command to:

kubectl --namespace "$namesp" exec "$pod" -c "$container" -- /bin/bash -c "builtin"

Would love to understand why the first command(s) don't work and the latter one does. I would have thought that builtin commands are the one group of commands that would always be found, in contrast to commands that rely on the PATH environment variable.


Solution

  • kubectl exec is used to execute an executable in a running container. The command has to be built into the container.

    Neither builtin nor cd are valid executables in your container. Only /bin/bash is.

    To execute a builtin shell command, you have to execute the shell and call it as the command argument like in your third example.