bash - Dockerfile RUN 命令是否在登录 shell 环境中运行?

标签 bash docker dockerfile

或者换句话说,我是否需要运行 docker run -it <container> bashdocker run -it <container> bash --login获得与它们运行时相同的环境?

最佳答案

to get the same environment as what they would run in?

我理解“因为它们会默认运行而无需指定命令

这将仅取决于您的容器实际配置为运行的方式:

  • 如果 ENTRYPOINTCMD 配置为运行登录 shell,您应该使用登录 shell
  • 如果 ENTRYPOINTCMD 配置为运行非登录 shell,您应该使用非登录 shell

您可以通过在您的容器上运行 docker inspectdocker image inspect 来识别它,这将为您提供 ENTRYPOINTCMD

如果您先运行 容器然后使用 docker exec -it bash [--login] 创建 shell,则原理相同

例如,使用这个 Dockerfile:

FROM alpine

RUN apk add bash
RUN echo "export MYVAR=frombashrc" > /root/.bashrc
RUN echo "export MYVAR=fromprofile" > /root/.bash_profile

ENTRYPOINT ["/bin/sh", "-c"]

并运行:

$ docker build . -t mybashimage
$ docker run -it --name bashcontainer mybashimage "bash --login -c 'env && sleep 60'"
HOSTNAME=4aeb776a8c56
MYVAR=fromprofile
...

在容器运行时在另一个 shell 中:

# Running a non-login shell does not have same effect
$ docker exec -it bashcontainer bash
bash-4.4# env
HOSTNAME=5f44398152bf
MYVAR=frombashrc
...

# Running login shell doe
$ docker exec -it bashcontainer bash --login -c 'env'
HOSTNAME=5f44398152bf
MYVAR=fromprofile
...

关于bash - Dockerfile RUN 命令是否在登录 shell 环境中运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57320549/

相关文章:

python - Bash 解析和连接执行 python 的参数

linux - 将多个文件名扩展到 bash 中的一个目录中

javascript - 如何从 VSCode 扩展执行本地 bash 代码

docker - 如何使用 Docker 的 COPY/ADD 指令将单个文件复制到镜像

linux - Bash 变量定义顺序

postgresql - Docker 等待 postgresql 运行

linux - 使用来自主机的 CA Trust Bundle 构建 Docker

docker - 在 Docker 终端 Windows 中更改目录

amazon-web-services - 检索AWS SM secret 并导出到容器环境变量

linux - 如何为给定 Linux 文件夹中的所有文件提供 777 默认权限