0

I am trying to add a init container to a deployment, but I can´t make the env vars work for some reason.

Just to illustrate, this is what I am trying:

initContainers:
    - name: init-db
      image: mysql:5.7
      env:
        - name: TEST
          value: nada
      args: ["echo", "${TEST}"]

But no matter what I do, the env vars doesn´t work, the echo always returns ${TEST} instead of nada

Any hint?

1
  • 1
    variable substitution is done by the shell. Run the command in a shell. Commented Oct 14, 2020 at 15:11

3 Answers 3

2

Like the comments and other answer mentioned you need to use shell for ENV substitution properly. therefore, replace the line

args: ["echo", "${TEST}"] in you code to

command: ['sh', '-c', 'echo ${TEST}']

Reference

Sign up to request clarification or add additional context in comments.

Comments

1

You need to use a shell in order to do ENV var substitution properly. For example:

initContainers:
    - name: init-db
      image: mysql:5.7
      env:
        - name: TEST
          value: nada
      command:
      - /bin/sh
      - -c
      - echo "${TEST}"

Comments

0

According to Kubernetes official documentation, you need to use parentheses $(TEST) for the variable to be expanded in the command or args field. I had an error using brackets because the env vars were not properly expanded on args. Working example:

initContainers:
  - name: my-container
    image: 'my-image'
    envFrom:
      - configMapRef:
          name: my-config
    args:
      - '-path=/$(MY_ENV_VAR)'

Kubernetes Documentation

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.