how to extend environment variable for a container in Kubernetes

Viewed 5853

Kubernetes documentation on setting environment variables of a container only include examples of new environment variables.

This approach does not work when I try to extend an existing environment variable PATH:

kind: Pod
apiVersion: v1
spec:
  containers:
    - name: blah
      image: blah
      env:
        - name: PATH
          value: "$PATH:/usr/local/nvidia/bin"

The created pod keeps crashing with

BackOff       Back-off restarting failed container
FailedSync    Error syncing pod

Any recommendations as to how I could extend the PATH environment variable?

3 Answers

If you expect the environment variable PATH to be updated with additional directory both for your container startup program and even when you want to get into the container using kubectl exec, you can update your manifest command argument something like below

spec:
  containers:
  - name: mysvc-daemon
    command:
    - /bin/bash
    - -c
    args: 
    - |
      echo 'export PATH=$PATH:$HOME/bin:$HOME/local/bin' >> $HOME/.bashrc

And then you can ensure to put the binary files in the directory $HOME/bin or $HOME/local/bin. If you get into the bash environment and check you should be able to see the directories $HOME/bin or $HOME/local/bin are now part of PATH environment variable

Related