0

My problem is the following:

I should execute the "envsubst" command from inside a POD, I'm using Kubernetes.

Actually I'm executing the command manually accessing to the POD and then executing it, but I would do it automatically inside my configuration file, which is a .yml file.

I've found some references on the web and I've tried some examples, but the result was always that the POD didn't start correctly, presenting the error CrashBackLoopOff error.

I would execute the following command:

envsubst < /usr/share/nginx/html/env_token.js > /usr/share/nginx/html/env.js

There's the content of my .yml file (not all, just the most relevant part)

spec:
  containers:
  - name: example 1
    image: imagename/docker_console:${deploy.version}
    env:
      - name: PIPPO_ID
        valueFrom:
          secretKeyRef:
            name: pippo-${deploy.env}-secret
            key: accessKey
      - name: PIPPO
        valueFrom:
          secretKeyRef:
            name: pippo-${deploy.env}-secret
            key: secretAccessKey
      - name: ENV
        value: ${deploy.env}
      - name: CREATION_TIMESTAMP
        value: ${deploy.creation_timestamp}
      - name: TEST
        value: ${consoleenv}
    command: ["/bin/sh"]
    args: ["envsubst", "/usr/share/nginx/html/assets/env_token.js /usr/share/nginx/html/assets/env.js"]

The final two rows, "command" and "args", should be written in this way? I've already tried to put the "envsubst" in the command but it didn't work. I've also tried using commas in the args row to separate each parameter, same error.

Do you have some suggestions you know they work for sure?

Thanks

  • Can't you configure your docker file to execute this command? Any specific reason to run this command from Kubernetes deployment file? – Nilucshan Siva Dec 10 '20 at 10:14
  • Hi Nilucshan, I've tried to put this instruction at the end of my Dockerfile: RUN envsubst < /usr/share/nginx/html/assets/env_token.js > /usr/share/nginx/html/assets/env.js but the result was that the solution didn't work and I explain you why. The instruction I'm talking about gets an environment variable which is set when the application is deployed on Kubernetes and sets a variable with that value. If I use the RUN command on the Dockerfile,at that moment the Kubernetes variable is still not set so I'll get an empty string and at the end I'll have a variable which is an empty string – Alessandro Violante Dec 10 '20 at 10:51
  • [nginx: use environment variables](https://stackoverflow.com/questions/21866477/nginx-use-environment-variables) describes a pure-Dockerfile setup. The `RUN` command will happen during your image build, before anything in the Kubernetes setup is considered at all. – David Maze Dec 10 '20 at 12:01
  • mmh...from what I've read, if I have understood it, I should do the following: COPY --from=builder /app/src/assets/env_token.js /usr/share/nginx/html COPY --from=builder /app/src/assets/env.js /usr/share/nginx/html CMD ["/bin/sh" , "-c" , "envsubst < /usr/share/nginx/html/assets/env_token.js > /usr/share/nginx/html/assets/env.js"] maybe I've not tried to use the "/bin/sh". Assuming I can't use Docker compose, I could write this under the "env" and try it out command: /bin/bash -c "envsubst < /usr/share/nginx/html/assets/env_token.js > /usr/share/nginx/html/assets/env.js" – Alessandro Violante Dec 10 '20 at 13:26
  • I've tried all these options and the POD always return errors... and with CMD also command: ["/bin/bash"] args: ["-c", "envsubst < /usr/share/nginx/html/assets/env_token.js > /usr/share/nginx/html/assets/env.js"] command: ["/bin/sh"] args: ["-c", "envsubst < /usr/share/nginx/html/assets/env_token.js > /usr/share/nginx/html/assets/env.js"] command: /bin/bash -c "envsubst < /usr/share/nginx/html/assets/env_token.js > /usr/share/nginx/html/assets/env.js" – Alessandro Violante Dec 10 '20 at 22:39
  • Hi, Can you try following. `command:[/bin/envsubst] args:[< /usr/share/nginx/html/assets/env_token.js>/usr/share/nginx/html/assets/env.js]` – Nilucshan Siva Dec 11 '20 at 07:44
  • Thank you, I've tried it, also with the double quotes, but unfortunately it doesn't work, I always get a POD error – Alessandro Violante Dec 11 '20 at 08:36
  • When you execute this command manually, do you use export command before using envsubst? [https://unix.stackexchange.com/questions/294378/replacing-only-specific-variables-with-envsubst/294400]https://unix.stackexchange.com/questions/294378/replacing-only-specific-variables-with-envsubst/294400 – Nilucshan Siva Dec 11 '20 at 11:36
  • Hi, nope, I execute this command: kubectl exec -ti my-pod-name sh and then when I'm inside, I execute this command: envsubst < /usr/share/nginx/html/assets/env_token.js > /usr/share/nginx/html/assets/env.js – Alessandro Violante Dec 15 '20 at 13:53
  • I understood it's a problem of how the envsubst function is written because if I use this command: command: ['sh', '-c', 'echo "Hello, Kubernetes!" && sleep 3600'] the POD starts without problems, if I use this command: command: ['sh', '-c', 'envsubst < /usr/share/nginx/html/assets/env_token.js > /usr/share/nginx/html/assets/env.js'] it breaks – Alessandro Violante Dec 15 '20 at 17:05

0 Answers0