ENV在Dockerfile中的位置是否对最终结果很重要?

发布于 2025-02-09 00:22:29 字数 220 浏览 0 评论 0原文

我的env在Dockerfile中的位置是否

ADD rootfs /
CMD ["/usr/sbin/MyApplication -V"]
ENV MY_APP_RUN_IN_TEST=true

很重要?例如,在上面的示例中,MyApplication是否可以运行并可以访问MY_APP_RUN_IN_TEST环境变量?

还是我需要在上面移动它?

Does the location of my ENV in Dockerfile:

ADD rootfs /
CMD ["/usr/sbin/MyApplication -V"]
ENV MY_APP_RUN_IN_TEST=true

matter ? For example in the above example, will MyApplication be run and have access to the MY_APP_RUN_IN_TEST environment variable ?

or do i need to move it above it ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤云独去闲 2025-02-16 00:22:29

在您的示例中,myApplication将可以访问环境变量。该顺序没关系。

我能想到的唯一方法是没有可用的env语句,即如果您拥有多阶段的Dockerfile。然后,ENV语句必须处于最后阶段才能包含在图像中。

例如,如果您有这样的dockerfile

FROM sdk as build
COPY . .
RUN build my app
ENV MY_ENV_VARIABLE=true

FROM runner
COPY --from=build /build/output .
CMD ["myapp"]

,则环境变量将不会可用。如果Env语句移至Dockerfile的最后阶段,则可以使用。

In your example, MyApplication will have access to the environment variable. The order doesn't matter.

The only way I can think of an ENV statement not being available, is if you have a multi-stage Dockerfile. Then the ENV statement would have to be in the final stage to be included in the image.

For instance, if you have a Dockerfile like this

FROM sdk as build
COPY . .
RUN build my app
ENV MY_ENV_VARIABLE=true

FROM runner
COPY --from=build /build/output .
CMD ["myapp"]

then the environment variable won't be available. If the ENV statement was moved to the last stage of the Dockerfile, then it would be available.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文