3

I have some problems with exporting an environment variable into docker Entrypoint file.

This is my docker file content:

   FROM ubuntu:16.04
   ADD entrypoint.sh .
   RUN chmod 777 entrypoint.sh
   ENTRYPOINT ["./entrypoint.sh"]
   CMD ["/bin/bash"]

In the Entrypoint file, I try to run command "export TOKEN=$client_token". Then, I create a container with that image file and I run "docker exec -it /bin/bash" command and into the container I continue run "set" command to show all environment variables. So,I can not find the $TOKEN variable that I exported before.

How can I export an environment variable into the entrypoint file?

stevenH
  • 155
  • 3
  • 13

1 Answers1

3

You must inject your host environment variable (client_token) into the docker container using '-e' when running:

docker run -it --rm -e client_token=<whatever> <your image>

This works for example with this kind of entrypoint:

#!/bin/bash
export TOKEN=$client_token
echo "The TOKEN is: ${TOKEN}"
# do stuff ...

If you don't know the token value when the container was run, you should inject during attachment (docker exec) and perform required operations inside, but probably it is not valid for you if running container already needed that information.

docker exec -it -e TOKEN=<whatever> <your container>

BRs

eramos
  • 196
  • 1
  • 16
  • Thanks for your help. So, The detail of my case is I have a entrypoint file contains the script run to get token value and assign it into an environment variable as a token and export it. After I created the container, the token environment variable not exist in my container. Is there have any solution to this? – stevenH Mar 07 '20 at 15:53
  • 2
    Sorry, i think i didn't understood actually the question. I will edit my answer. As far i understand, your host environment knows 'client_token' value in the moment of running the container. You have to inject client_token then. – eramos Mar 08 '20 at 11:25
  • Thanks for your support @eramos. That problem has been resolved. Again, I want to say thank you. – stevenH Mar 09 '20 at 16:21
  • 1
    Thank u, you are the first person I help in stackoverflow ;) it is a pleasure. Normally i get help from here, everyday! BRs – eramos Mar 09 '20 at 16:56
  • 1
    Could you update with the solution to this problem? – jecabeda Jan 04 '21 at 16:46