简体   繁体   English

基于构建 ARG 有条件地在 Dockerfile 中设置 ENV 变量

[英]Conditionally setting ENV vars in Dockerfile based on build ARG

I'd like to conditionally set some ENV vars in my Dockerfile based on certain build ARGs.我想根据某些构建 ARG 有条件地在我的 Dockerfile 中设置一些 ENV 变量。 For one reason or another, the ENV var doesn't map directly to the actual build ARG ie I can NOT do the following:出于某种原因,ENV 变量不会直接映射到实际的构建 ARG,即我不能执行以下操作:

ARG myvar
ENV MYVAR $MYVAR

It's complex enough a mapping that I can't just do, like, "prefix-${$MYVAR}", or whatever, either.这是一个足够复杂的映射,我不能只做,比如“前缀-${$MYVAR}”,或者其他任何东西。

I'm aware of just using a shell command with EXPORT instead (and then having access to if statements), but exporting the environmental variable this way won't persist across containers the way I need to.我知道只使用带有 EXPORT 的 shell 命令(然后可以访问 if 语句),但是以这种方式导出环境变量不会像我需要的那样在容器中持续存在。 If the only solution is to just repeatedly prefix the needed env for every RUN/CMD I need it for (and all the logic needed to get there), then I would accept that.如果唯一的解决方案是为我需要的每个 RUN/CMD 重复添加所需的 env 前缀(以及到达那里所需的所有逻辑),那么我会接受。

I'm also aware of this answer Conditional ENV in Dockerfile , which does have one solution where a (essentially) ternary is used to trigger a certain ENV value, but because my situation is more complex, I can't just use that given solution.我也知道Dockerfile 中的这个答案Conditional ENV ,它确实有一个解决方案,其中使用(基本上)三元来触发某个 ENV 值,但是因为我的情况更复杂,我不能只使用给定的解决方案.

Is there a way to write "logic" inside Dockerfiles, while still having access to Docker commands like ENV?有没有办法在 Dockerfiles 中编写“逻辑”,同时仍然可以访问像 ENV 这样的 Docker 命令?

Any help would be really appreciated!任何帮助将非常感激!

PS.附注。

This is a Dockerfile to build a Node image, so my last few steps look basically like这是一个用于构建 Node 镜像的 Dockerfile,所以我的最后几步基本上是这样的

RUN npm run build
CMD ["npm", "run", "start"]

If I understand your question correctly, you want如果我正确理解你的问题,你想要

if BUILD_VAR == v1:
  set ENV MYVAR=aValue
else if YOUR_VAR == v2:
  set ENV MYVAR=anotherValue

Solution:解决方案:

ARG BUILD_VAR=v1

FROM scratch as build_v1
ONBUILD ENV MYVAR=aValue

FROM scratch as build_v2
ONBUILD ENV MYVAR=anotherValue

FROM build_${YOUR_VAR} AS final
...
RUN npm run build
CMD ["npm", "run", "start"]

The toplevel BUILD_VAR allows you to pass the condition with docker build --build-arg BUILD_VAR=v1 .顶层BUILD_VAR允许您使用docker build --build-arg BUILD_VAR=v1 .传递条件docker build --build-arg BUILD_VAR=v1 .

ONBUILD is used so the instruction ENV is only executed if the corresponding branch is selected. ONBUILD ,因此只有在选择了相应的分支时才会执行指令ENV

I had a similar issue for setting proxy server on a container.我在容器上设置代理服务器时遇到了类似的问题。

The solution I'm using is an entrypoint script, and another script for environment variables configuration.我使用的解决方案是一个入口点脚本和另一个用于环境变量配置的脚本。 Using RUN, you assure the the configuration script runs on build, and ENTRYPOINT when you run the container.使用 RUN,您可以确保配置脚本在构建时运行,并在运行容器时运行 ENTRYPOINT。

The entrypoint script looks like:入口点脚本如下所示:

#!/bin/bash
    
# Run any other commands needed 

# Run the main container command
exec "$@"

And in the Dockerfile, configure:在 Dockerfile 中,配置:

#copy bash scripts
COPY configproxy.sh /root
COPY startup.sh .    
RUN ["/bin/bash", "-c", ". /root/configproxy.sh"]
ENTRYPOINT ["./entrypoint.sh"]
CMD ["sh", "-c", "bash"]

Take a look here .看看这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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