Docker参数为RUN echo命令 [英] Docker argument to a RUN echo command
问题描述
作为Dockerfile的一部分,我试图修改文本文件(ssh_config)以包含用户(作为ARG)在构建时传递给Docker容器的变量。
As part of a Dockerfile, I am attempting to modify a text file (ssh_config) to contain a variable passed through by the user (as an ARG) to the docker container at build time.
在我的Dockerfile中(不是整个文件):
In my Dockerfile I have (this is not the entire file):
ARG key_name
RUN echo 'Host geoserver\n\
User user\n\
HostName 38.191.191.111\n\
IdentityFile /root/$key_name' >> /etc/ssh/ssh_config
这将收集参数key_name,然后将一些文本附加到ssh_config文本中
This collects the argument key_name, and then appends some text to the ssh_config text file.
运行如下:
docker build --build-arg key_name=GC -t pyramid .
当我检查看写了什么内容时,尚未解析key_name变量,而是已被写为文本(也就是$ key_name)。显然,我希望将其替换为通过ARG传递的变量。
When I check to see what has been written, the key_name variable hasn't been parsed, and instead has been written as text (so literally as $key_name). Obviously I want it to be replaced with the variable passed through ARG.
我尝试使用 $ {key_file}
而不只是 $ key_file
,我在文本文件中得到的是相同的文本,但包括花括号。
I have tried using ${key_file}
instead of just $key_file
, I just get the same text in the text file but with curly braces included.
所以我的问题是,如何在RUN echo语句中正确使用ARG变量?
So my question is, how can I use the ARG variable correctly within the RUN echo statement?
推荐答案
首先:请确保您的 ARG
在您的 FROM
之后出现。请参阅: https://docs.docker。 com / engine / reference / builder /#understand-how-arg-and-from-interact
First: Make sure, your ARG
comes after your FROM
. See: https://docs.docker.com/engine/reference/builder/#understand-how-arg-and-from-interact
第二个:正如您在此处看到的那样,变量不会在内解释。
,因此请使用
。
Second: As you can see here, variables won't be interpretad inside ''
, so use ""
instead.
这篇关于Docker参数为RUN echo命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!