MIx environment variables from kubernetes deployment and docker image

6/26/2020

I am trying to pass an environment variable in my deployment that should define a prefix based on a version number:

env:
  - name: INDEX_PREFIX
    value: myapp-$(VERSION)

$(VERSION) is not defined in my deployment but is set in the docker image used by the pod. I tried to use both $() and ${} but VERSION is not interpolated in the environment of my pod. In my pod shell doing export TEST=myapp-${VERSION} does work though.

Is there any way to achieve what I am looking for? ie setting an environment variable in my deployment that reference an environment variable set in the docker image?

-- ITChap
environment-variables
kubernetes

1 Answer

6/26/2020

VERSION is an environment variable of the docker image. So you can assign it a value either inside the container or by passing

env: 
   - name : VERSION
     value : YOUR-VALUE

In your case, VERSION is either set by a script inside the docker container or in the Dockerfile.

You can do :

  1. In the Dockerfile, adding ENV INDEX_PREFIX myapp-${VERSION}
  2. Adding a script to your entrypoint as export INDEX_PREFIX=myapp-${VERSION}

In case you can't modify Dockerfile, you can try to :

  • Get the image entrypoint file from the docker image (ie: /IMAGE-entrypoint.sh) and the image args(ie: IMAGE-ARGS). you can use docker inspect IMAGE.
  • Override the container command and args in the pod spec using a script.
   command:
           - '/bin/sh'
   args:
           - '-c'
           - |
             set -e
             set -x
             export INDEX_PREFIX=myapp-${VERSION} 
             IMAGE-entrypoint.sh IMAGE-ARGS

k8s documentation : https://kubernetes.io/docs/tasks/inject-data-application/define-command-argument-container/ Hope it could help you.

-- wassim TRIFI
Source: StackOverflow