Docker echo environment variable

前端 未结 3 1223
刺人心
刺人心 2021-02-19 13:56

I\'m trying to write a little docker file that sets a User and just echos the current user as a little example to prove to myself it is working. I\'ve tried a number of variants

3条回答
  •  無奈伤痛
    2021-02-19 14:16

    This is the expected behavior, as weird as it seems!

    When ENTRYPOINT is a list (as in ENTRYPOINT ["echo", "$USER"]), it is used as-is, without further parsing or interpretation. So $USER remains $USER, because there is no shell involved in the process to replace it with the value of the USER environment variable.

    Now, when ENTRYPOINT is a string (as in ENTRYPOINT echo $USER), what is actually executed is sh -c "echo $USER", and $USER is replaced with the value of the environment variable (as you would expect).

    However, the environment variable USER is not set by default. It is set by the login process; and when you just run sh -c ... the login process is not involved.

    Compare the environment when running docker run -t -i ubuntu bash and docker run -t -i ubuntu login -f root. In the former case, you will get a very basic environment; in the latter case, you will get the complete environment that you are used to (including USERvariable).

提交回复
热议问题