Search code examples
dockerkubectl

How to exec kubectl command on pod using pod-local environment variables?


I am trying to run command that needs to use environment variables specific to the container ($JAVA_HOME), but it seems that variable is expanded on my local shell before sending command to the pod container

kubectl exec my-pod "echo ${JAVA_HOME}"

produces:

Use 'kubectl describe pod/my-pod -n default' to see all of the containers in this pod.
OCI runtime exec failed: exec failed: container_linux.go:345: starting container process caused "exec: \"echo \": executable file not found in $PATH": unknown
command terminated with exit code 126

I tried several syntax without luck (e.g. bash -c). How to escape the variable so that it is evaluated on the target container?


Solution

  • Try this:

    kubectl exec my-pod -- bash -c 'echo $JAVA_HOME'