dockerfile do not s source .bashrc即使在一个子壳中

发布于 2025-01-18 11:26:39 字数 1109 浏览 0 评论 0原文

我正在尝试获取 .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 技术交流群。

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

发布评论

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

评论(3

玩物 2025-01-25 11:26:39

通常,〜/.bashrc包含类似的内容:

# If not running interactively, don't do anything
case $- in
    *i*) ;;
      *) return;;
esac

非常正常 - .bashrc仅在交互式会话中使用。因为运行是非相互作用的,所以它只是退出。

我建议,如果您只想添加环境变量,请将它们输出到/etc/profile.d。 /etc/profile

Usually ~/.bashrc contains something similar to:

# If not running interactively, don't do anything
case $- in
    *i*) ;;
      *) return;;
esac

That is very normal - .bashrc is meant to be used in interactive sessions only. Because RUN 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.

断桥再见 2025-01-25 11:26:39

Docker 中的大多数路径根本不读取 shell 点文件。您需要使用其他方法来为您的应用程序提供配置;例如,如果您需要在启动容器之前动态设置内容,则可以使用 Dockerfile ENV 设置环境变量或入口点包装器脚本。

让我们具体看一下示例的简化形式:

SHELL ["/bin/bash", "-c"]
RUN echo "export TEST_VAR=test" >> $HOME/.bashrc
RUN echo "$TEST_VAR"

Bash GNU Bash 手册中的启动文件列出了在哪种情况下读取哪些点文件。对于最后一行,Docker 结合了 SHELLRUN 行来运行等效项,

/bin/bash -c 'echo "$TEST_VAR"'

bash 实例既不是交互式 shell,也不是登录 shell,因此,唯一自动读取的点文件是在 $BASH_ENV 环境变量中命名的点文件。 (POSIX sh 未指定关于任何 shell 点文件的任何内容。)

这进一步适用于图像的默认 CMD,它也将使用 sh -c (或替代的SHELL)并且它不会读取点文件。如果 CMD (或 ENTRYPOINTRUN)使用 JSON 数组语法,它根本不会调用 shell,并且再次不会' t 读取点文件。

读取 shell 点文件的唯一情况是主容器命令是交互式 shell,这通常不是常见情况。

docker run --rm -it yourimage /bin/bash         # reads .bashrc
docker run --rm -it yourimage /bin/bash --login # also reads .profile, .bash_login

这意味着您几乎不应该尝试编辑 .bashrc/etc/profile 或任何类似文件。如果您需要像示例中那样设置环境变量,请改用 Dockerfile ENV

ENV TEST_VAR=test
RUN echo "$TEST_VAR"

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:

SHELL ["/bin/bash", "-c"]
RUN echo "export TEST_VAR=test" >> $HOME/.bashrc
RUN echo "$TEST_VAR"

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 and RUN lines to run the equivalent of

/bin/bash -c 'echo "$TEST_VAR"'

but 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. (POSIX sh doesn't specify anything about any shell dotfiles at all.)

This further applies to the image's default CMD, which also will get run with sh -c (or the alternate SHELL) and it won't read dotfiles. If the CMD (or ENTRYPOINT or RUN) 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.

docker run --rm -it yourimage /bin/bash         # reads .bashrc
docker run --rm -it yourimage /bin/bash --login # also reads .profile, .bash_login

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 Dockerfile ENV instead.

ENV TEST_VAR=test
RUN echo "$TEST_VAR"
瑾兮 2025-01-25 11:26:39

基于问题

FROM ubuntu

SHELL ["/bin/bash", "-c"]
ENV BASH_ENV="/etc/bash_env"
RUN echo "export TEST_VAR=test" >> $BASH_ENV

RUN echo $TEST_VAR
#6 [3/3] RUN echo $TEST_VAR
#6 0.239 test
#6 DONE 0.3s

参考

Based on the comments from the question

FROM ubuntu

SHELL ["/bin/bash", "-c"]
ENV BASH_ENV="/etc/bash_env"
RUN echo "export TEST_VAR=test" >> $BASH_ENV

RUN echo $TEST_VAR
#6 [3/3] RUN echo $TEST_VAR
#6 0.239 test
#6 DONE 0.3s

Reference

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