kubernetes

passing env variable to docker image from k8 secret store


How to expand environment variables coming from a secret store and pass them inside a docker container?. Said docker container does not have a shell, therefore it is not possible to run a script. This is the sample yaml file

        envFrom:
        - secretRef:
            name: secret     
        command: ["my-command"]
        args:
          - "--env=ENV1=${MY_ENV_VAR1}"
          - "--env=env2=${MY_ENV_VAR2}"


Solution

  • You can pass environment variables to your arguments by using parentheses () instead of braces {}

        envFrom:
        - secretRef:
            name: secret     
        command: ["my-command"]
        args:
          - "--env=ENV1=$(MY_ENV_VAR1)"
          - "--env=env2=$(MY_ENV_VAR2)"
    

    Kubernetes docs have an example here for reference: https://kubernetes.io/docs/tasks/inject-data-application/define-command-argument-container/#use-environment-variables-to-define-arguments