我可以在Dockerfile USER语句中使用环境变量吗? [英] Can I use an environment variable in a Dockerfile USER statement?
问题描述
所以我的Dockerfile是这样写的:
So my Dockerfile is written like so:
# set env vars for linux user
ENV LINUX_USER="kbuser"
...
... # other stuff
...
USER kbuser
但是我想使用
USER $LINUX_USER
这样,我只需要在文件的一个位置写入用户名即可。但这不起作用。
So that I only have to write the username in one place in the file. But this doesn't work.
我该如何解决?
谢谢。
推荐答案
使用 ARG
指令。根据文档:
Use the ARG
directive. As per docs:
ARG< name> [=<默认值>]
ARG指令定义了一个变量,用户可以在构建时使用-build-arg< varname> =< ; value>
标志。如果用户指定了未在Dockerfile中定义的构建参数,则构建会输出警告。
The ARG instruction defines a variable that users can pass at build-time to the builder with the docker build command using the --build-arg <varname>=<value>
flag. If a user specifies a build argument that was not defined in the Dockerfile, the build outputs a warning.
因此,您实际上可以使用 ARG
像这样:
So you can actually use ARG
like so:
ARG user=kbuser
USER $user
您实际上可以在构建docker映像时设置参数:
And you can actually set the argument when building the docker image:
docker build --build-arg user=banana .
文档中还有更多内容,因此您最好通读。
There is much more to it in the docs, so you better read thoroughly.
这篇关于我可以在Dockerfile USER语句中使用环境变量吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!