简体   繁体   中英

Docker Ubuntu environment variables

During the build stage of my docker images, i would like to set some environment variables automatically for every subsequent "RUN" command.

However, I would like to set these variables from within the docker conatiner, because setting them depends on some internal logic.

Using the dockerfile "ENV" command is not good, because that cannot rely on internal logic. (It cannot rely on a command run inside the docker container)

Normally (if this were not docker) I would set my ~/.profile file. However, docker does not load this file in non-interactive shells.

So at them moment I have to run each docker RUN command with:

RUN bash -c "source ~/.profile && do_something_here"

However, this is very tedious (and unclean) when I have to repeat this every time I want to run a bash command. Is there some other "profile" file I can use instead.

您可以尝试在ARG my_env中将 arg 设置为 env,例如ARG my_env ENV my_env=${my_env} ,并在 build-args 中传递“my_env=prod”,以便您可以将 set env 用于后续的 RUN 命令,您也可以使用env_file:在堆栈部署的情况下, env_file: compose yml 文件中的选项

I had a similar problem and couldn't find a satisfactory solution. What I did was creating a script that would source the variables, then do the operation. I would then rewrite the RUN commands in the Dockerfile to use that script instead.

In your case, if you need to run multiple commands, you could create a wrapper that loads the variables, runs the command given as argument, and include that script in the docker image.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM