简体   繁体   English

Dockerfile - 获取、修改和设置环境变量

[英]Dockerfile - get, modify and set env variable

probably really simple question, but I cannot find a solution.可能非常简单的问题,但我找不到解决方案。 I have some env variable what stores some version number.我有一些环境变量存储一些版本号。 At this moment it's just 11.1.1.目前它只是 11.1.1。 I need to modify it to get 11.1 and save to env ABC_VER.我需要修改它以获取 11.1 并保存到 env ABC_VER。 So it's possible to do it after building my container like below:因此,可以在构建我的容器后执行此操作,如下所示:

export ABC=$(echo $ABC_VERSION | head -c 4)

I was trying to do the same in my dockerfile:我试图在我的 dockerfile 中做同样的事情:

ENV ABC_VER $(echo $ABC_VERSION | head -c 4)

But after building and running echo $ABC_VER I got:但是在构建并运行 echo $ABC_VER 之后,我得到了:

$(echo 11.1.1 | head -c 4)

instead of 11.1而不是 11.1

Not sure, how to fix it?不确定,如何解决?

Docker's ENV instruction is a bit limited in this sense. Docker 的ENV指令在这个意义上有点受限。 As far as I know, the most advanced thing you can do is environment replacement , but that won't be of much help here.据我所知,您可以做的最先进的事情是环境替换,但这在这里没有太大帮助。

I can think of two low-hanging options:我可以想到两个低悬的选择:

  1. Use RUN to evaluate the expression you're using and appending an export instruction with the result to the appropriate shell startup script (eg, /etc/profile (or preferably a separate file under /etc/profile.d ))使用RUN来评估您正在使用的表达式,并将带有结果的export指令附加到相应的 shell 启动脚本(例如/etc/profile (或者最好是/etc/profile.d下的单独文件))
  2. Use ARG MYARG in the Dockerfile and provide the correctly formatted version as a --build-arg and use ENV ABC_VER=${MYARG} .在 Dockerfile 中使用ARG MYARG Dockerfile提供格式正确的版本作为--build-arg并使用ENV ABC_VER=${MYARG}

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

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