admin管理员组文章数量:1122832
I am building a Docker image from the OCaml
image ocaml/opam:debian-12-ocaml-5.2
.
During build phase, I need to access the env variable OPAM_SWITCH_PREFIX
which is defined to be /home/opam/.opam/5.2
in the base image above. However, it seems it is not accessible to the commands in my Dockerfile
.
Any idea why is that and how to fix it (I would preferably not hard code the variable in my Dockerfile
nor have to pass it each time with -build-arg
?
I am building a Docker image from the OCaml
image ocaml/opam:debian-12-ocaml-5.2
.
During build phase, I need to access the env variable OPAM_SWITCH_PREFIX
which is defined to be /home/opam/.opam/5.2
in the base image above. However, it seems it is not accessible to the commands in my Dockerfile
.
Any idea why is that and how to fix it (I would preferably not hard code the variable in my Dockerfile
nor have to pass it each time with -build-arg
?
1 Answer
Reset to default 0When the user's Dockerfile runs commands (e.g., RUN opam ...), these commands are executed in a new shell session within the container during the build process. While the base image sets the environment variable, this setting doesn't automatically persist across all subsequent RUN instructions in the Dockerfile. Each RUN instruction starts a fresh shell, so the variable isn't automatically inherited. You can use ENV instrcution inside Dockerfile. The ENV instruction sets environment variables that persist across all subsequent RUN instructions within the Dockerfile.
FROM ocaml/opam:debian-12-ocaml-5.2
# Set the environment variable using ENV
ENV OPAM_SWITCH_PREFIX="/home/opam/.opam/5.2"
# Now OPAM_SWITCH_PREFIX is available in this RUN command
RUN echo "OPAM_SWITCH_PREFIX is: $OPAM_SWITCH_PREFIX"
# And also in this one
RUN opam switch list
本文标签: linuxDockerHow to access base image environment variables at build timeStack Overflow
版权声明:本文标题:linux - Docker : How to access base image environment variables at build time - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736281126a1926222.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
--build-arg
) – knittl Commented yesterdayFROM bash ENV myvar=42
, build withdocker build -t base .
, derived file:FROM base RUN echo $myvar
. Build output:42
. Run the derived image and enterecho $myvar
in the shell – output:42
. – knittl Commented yesterday