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?
When you create a Pod, you can set environment variables for the containers that run in the Pod. To set environment variables, include the env or envFrom field in the configuration file. In your shell, run the printenv command to list the environment variables. To exit the shell, enter exit .
To set environment variables, include the env or envFrom field in the configuration file. Note: The environment variables set using the env or envFrom field override any environment variables specified in the container image.
How to set the Environment variable in the container? To set an environment variable you should use flag -e while using docker run or docker-compose command. Environment file - If the environment variable is not overridden by docker-compose.
Short answer is No, they can't.
If you only need this path declaration for the command you are running with, you can add it to containers
section, under args
Example:
spec:
containers:
- name: blah
image: blah
args:
- PATH="$PATH:/usr/local/nvidia/bin" blah
If you do not have args specified in your yaml, you probably have a CMD specified in your Dockerfile that will just run your container with the command automatically. Thus you can add the following to your Dockerfile.
CMD ["PATH=$PATH:/usr/local/nvidia/bin", "blah"]
If you want this to be in your container in general, you would have to add to the .profile or .bashrc file of the user within the container you are using. This will probably involve creating a new image with these new files baked in.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With