I would like to know if we can update environment variables dynamically. I've used below command to update the APPLICATIONINSIGHTS_SAMPLING_PERCENTAGE locally. It worked locally and pod name has the new environment variable but it is not reflecting in the Container environment variables and not updating the Sampling percentage to 50% as well. I would like to understand, is there a way to dynamically update environment variable? Please explain.
kubectl exec -it <PODNAME> -- bash -c 'export APPLICATIONINSIGHTS_SAMPLING_PERCENTAGE=50; echo $APPLICATIONINSIGHTS_SAMPLING_PERCENTAGE'
FYI:
can I set container environment varaible dynamically at runtime ?
In a typical containerized application, environment variables are often set during the container startup, and their values are fixed throughout the container's lifecycle. However, if you need to dynamically update environment variables at runtime within a running container, it becomes a more complex task and may not be the recommended approach due to various reasons, including security and immutability principles associated with containers.
Here's an example of how you might set environment variables dynamically at runtime within a container:
docker exec -it <container_id> bash -c 'export MY_VARIABLE=new_value && echo $MY_VARIABLE'
Explanation:
docker exec -it <container_id> : This command allows you to execute a command inside a running container interactively.
bash -c 'export MY_VARIABLE=new_value && echo $MY_VARIABLE' : This command is executed inside the container. It exports a new value for the MY_VARIABLE environment variable and then prints the variable.