或者换句话说,我是否需要运行 docker run -it <container> bash
或 docker run -it <container> bash --login
获得与它们运行时相同的环境?
最佳答案
to get the same environment as what they would run in?
我理解“因为它们会默认运行而无需指定命令”
这将仅取决于您的容器实际配置为运行的方式:
- 如果
ENTRYPOINT
或CMD
配置为运行登录 shell,您应该使用登录 shell - 如果
ENTRYPOINT
或CMD
配置为运行非登录 shell,您应该使用非登录 shell
您可以通过在您的容器上运行 docker inspect
或 docker image inspect
来识别它,这将为您提供 ENTRYPOINT
和 CMD
如果您先运行
容器然后使用 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/