当我 docker 运行 --mount bashrc 时,bashrc 文件不工作

the bashrc file is not working when I docker run --mount bashrc

我正在 docker(搜索引擎)上测试一个应用程序,但是当我使用 docker runbashrc 不起作用,例如 bashrc,我不能用。 文件bashrc复制到容器中,但仍然不能使用。 我的问题是为什么不呢?仅仅是因为 bashrc 需要重新加载还是有其他原因?

sudo docker run \
     --mount type=bind,source=$(pwd)/remise/bashrc,destination=/root/.bashrc,readonly \
     --name="s-container" \
     ubuntu /go/bin/s qewrty

如果您将容器启动为

docker run ... image-name \
  /go/bin/s qwerty

当Docker创建容器时,它直接运行命令/go/bin/s qwerty;它不会调用 bash 或任何其他 shell 来执行此操作。没有人会知道要查找 .bashrc 文件。

同样,如果您的Docker文件指定

CMD ["/go/bin/s", "qwerty"]

它直接运行命令而不需要 shell。

CMD 有另一种 shell 形式,它接受一个命令字符串,并通过 /bin/sh -c 运行它。 确实涉及shell;但它既不是交互式也不是登录 shell,并且它被调用为 sh,因此它不会读取任何 shell 点文件(对于 /bin/sh 发生的特定情况是 GNU Bash,参见 Bash Startup Files)。

由于 none 这些用于指定主容器命令的公共路径将读取 .bashrc 或其他 shell 点文件,因此尝试写入或注入通常没有意义这些文件。如果您需要设置环境变量,请考虑使用 Dockerfile ENV 指令或入口点包装脚本。