Docker 容器内交互式 shell 上的源脚本



我想打开一个交互式 shell,它源一个脚本来使用我绑定挂载的存储库上的 bitbake 环境:

docker run --rm -it 
--mount type=bind,source=$(MY_PATH),destination=/mnt/bb_repoistory  
my_image /bin/bash -c "cd /mnt/bb_repoistory/oe-core && source build/conf/set_bb_env.sh"

问题是-it参数似乎没有任何效果,因为 shell 在执行后立即退出cd /mnt/bb_repoistory/oe-core && source build/conf/set_bb_env.sh

我也试过这个:

docker run --rm -it 
--mount type=bind,source=$(MY_PATH),destination=/mnt/bb_repoistory  
my_image /bin/bash -c "cd /mnt/bb_repoistory/oe-core && source build/conf/set_bb_env.sh && bash"

这会生成一个交互式 shell,但没有定义任何宏set_bb_env.sh

有没有办法提供具有正确来源的脚本的 tty?

-it标志与要运行的命令冲突,因为您告诉 docker 创建伪终端 (ptty(,然后在该终端中运行命令 (bash -c ...(。 该命令完成后,运行完成。

有些人为解决此问题所做的是在其源环境中仅具有export变量,最后一个命令将是exec bash。但是,如果您需要别名或其他不是这样继承的项目,那么您的选择会更加有限。

您可以在目标 shell 中运行源代码,而不是在父 shell 中运行源代码。如果您修改了.bash_profile以包含以下行:

[ -n "$DOCKER_LOAD_EXTRA" -a -r "$DOCKER_LOAD_EXTRA" ] && source "$DOCKER_LOAD_EXTRA”

然后让你的命令是:

... /bin/bash -c "cd /mnt/bb_repository/oe-core && DOCKER_LOAD_EXTRA=build/conf/set_bb_env.sh exec bash"

那可能行得通。这会告诉您的.bash_profile在 env 变量已设置时加载此文件,否则不会。(docker 命令行上也可以有-e标志,但我认为这会为整个容器全局设置它,这可能不是您想要的。

最新更新