Search code examples
dockerenvironment-variablesentry-pointdocker-entrypoint

Exporting a environment variable in Entrypoint file not work?


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?


Solution

  • 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