dockerfile do not s source .bashrc即使在一个子壳中
我正在尝试获取 .bashrc
的源代码,但没有运气
USER user
SHELL ["/bin/bash", "-c"]
RUN echo "export TEST_VAR=test" >> /home/user/.bashrc && tail /home/user/.bashrc && source /home/user/.bashrc && echo "1 \"${TEST_VAR} 2\" var" && exit 1
,我希望这个 RUN
命令打印 1 "test" 2
但我得到的是
Step 13/40 : RUN echo "export TEST_VAR=test" >> /home/user/.bashrc && tail /home/user/.bashrc && source /home/user/.bashrc && echo "1 \"${TEST_VAR}\" 2" && exit 1
---> Running in b870d36e9dd0
# this, if it's already enabled in /etc/bash.bashrc and /etc/profile
# sources /etc/bash.bashrc).
if ! shopt -oq posix; then
if [ -f /usr/share/bash-completion/bash_completion ]; then
. /usr/share/bash-completion/bash_completion
elif [ -f /etc/bash_completion ]; then
. /etc/bash_completion
fi
fi
export TEST_VAR=test
1 "" 2
在 docker 中处理 shell 有什么问题?我只想源 ~/.bashrc
一次,并在 source
调用下面的后续命令中使用所有公开的变量,但它甚至无法在与 & 连接的单个子 shell 中工作。 &
I'm trying to source .bashrc
but no luck
USER user
SHELL ["/bin/bash", "-c"]
RUN echo "export TEST_VAR=test" >> /home/user/.bashrc && tail /home/user/.bashrc && source /home/user/.bashrc && echo "1 \"${TEST_VAR} 2\" var" && exit 1
I expect that this RUN
command print 1 "test" 2
but what i get is that
Step 13/40 : RUN echo "export TEST_VAR=test" >> /home/user/.bashrc && tail /home/user/.bashrc && source /home/user/.bashrc && echo "1 \"${TEST_VAR}\" 2" && exit 1
---> Running in b870d36e9dd0
# this, if it's already enabled in /etc/bash.bashrc and /etc/profile
# sources /etc/bash.bashrc).
if ! shopt -oq posix; then
if [ -f /usr/share/bash-completion/bash_completion ]; then
. /usr/share/bash-completion/bash_completion
elif [ -f /etc/bash_completion ]; then
. /etc/bash_completion
fi
fi
export TEST_VAR=test
1 "" 2
What's wrong with handling shells in docker? I just wanted to source ~/.bashrc
once and use all exposed variables in subsequent command below source
call but it doesn't even work in a single subshell joined with &&
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
通常,
〜/.bashrc
包含类似的内容:非常正常 -
.bashrc
仅在交互式会话中使用。因为运行
是非相互作用的,所以它只是退出。我建议,如果您只想添加环境变量,请将它们输出到
/etc/profile.d
和。 /etc/profile
。Usually
~/.bashrc
contains something similar to:That is very normal -
.bashrc
is meant to be used in interactive sessions only. BecauseRUN
is non-interactive, it just exits.Aaaanyway, I would recommend, if you want to only add environment variables, output them to
/etc/profile.d
and. /etc/profile
.Docker 中的大多数路径根本不读取 shell 点文件。您需要使用其他方法来为您的应用程序提供配置;例如,如果您需要在启动容器之前动态设置内容,则可以使用 Dockerfile
ENV
设置环境变量或入口点包装器脚本。让我们具体看一下示例的简化形式:
Bash GNU Bash 手册中的启动文件列出了在哪种情况下读取哪些点文件。对于最后一行,Docker 结合了
SHELL
和RUN
行来运行等效项,但
bash
实例既不是交互式 shell,也不是登录 shell,因此,唯一自动读取的点文件是在 $BASH_ENV 环境变量中命名的点文件。 (POSIXsh
未指定关于任何 shell 点文件的任何内容。)这进一步适用于图像的默认
CMD
,它也将使用sh -c
(或替代的SHELL
)并且它不会读取点文件。如果CMD
(或ENTRYPOINT
或RUN
)使用 JSON 数组语法,它根本不会调用 shell,并且再次不会' t 读取点文件。读取 shell 点文件的唯一情况是主容器命令是交互式 shell,这通常不是常见情况。
这意味着您几乎不应该尝试编辑
.bashrc
、/etc/profile
或任何类似文件。如果您需要像示例中那样设置环境变量,请改用 DockerfileENV
。Most paths in Docker don't read shell dotfiles at all. You need to use other approaches to provide configuration to your application; for example, Dockerfile
ENV
to set environment variables or an entrypoint wrapper script if you need things to be set up dynamically before starting the container.Let's look specifically at a reduced form of your example:
Bash Startup Files in the GNU Bash manual lists out which dotfiles are read in which case. For the last line Docker combines the
SHELL
andRUN
lines to run the equivalent ofbut the
bash
instance is neither an interactive nor a login shell, so the only dotfile that's automatically read is one named in a$BASH_ENV
environment variable. (POSIXsh
doesn't specify anything about any shell dotfiles at all.)This further applies to the image's default
CMD
, which also will get run withsh -c
(or the alternateSHELL
) and it won't read dotfiles. If theCMD
(orENTRYPOINT
orRUN
) uses JSON-array syntax, it won't invoke a shell at all, and again won't read dotfiles.The only case where shell dotfiles will be read is if the main container command is an interactive shell, and this won't typically be the common case.
This means you should almost never try to edit the
.bashrc
,/etc/profile
, or any similar files. If you need to set environment variables as in the example, use DockerfileENV
instead.基于问题
参考
Based on the comments from the question
Reference