如何完全像在Dockerfile中一样运行Docker cmds [英] How do I Run Docker cmds Exactly Like in a Dockerfile
问题描述
在启动容器之后,Docker在Dockerfile中运行命令的方式与手动运行命令之间似乎有所不同.这似乎是由于您可以启动的外壳类型所致,是一种(我认为)具有Dockerfile的非交互式外壳,而在运行 docker run -it< some-img-id>
.
There seems to be a difference between how Docker runs commands in a Dockerfile versus running commands manually after starting a container. This seems to be due to the kind of shells you can start, a (I assume) non-interactive shell with a Dockerfile vs an interactive one when running something like docker run -it <some-img-id>
.
如何调试Docker容器中正在运行的命令,使其运行与从Dockerfile运行的命令完全相同?将/bin/bash --noprofile
添加到 run
cmd就足够了吗?或者从Dockerfile启动时,环境是否还有其他区别?
How can I debug running commands in a Docker container so that it runs exactly like the commands are run from a Dockerfile? Would just adding /bin/bash --noprofile
to the run
cmd suffice? Or is there anything else different about the environment when started from a Dockerfile?
推荐答案
您正在体验的是由于外壳而引起的行为.我们大多数人都习惯于使用bash shell.因此,通常我们会尝试以以下方式运行命令
What you are experiencing is the behavior because of the shell. Most of us are used to using the bash shell. So generally we would attempt to run the commands in the below fashion
对于新容器
docker run -it <imageid> bash
对于现有容器
docker exec -it <containerid> bash
但是当我们在Dockerfile中使用 RUN
指令指定某些命令时
But when we specify some command using RUN
directive inside a Dockerfile
RUN echo Testing
然后等同于运行/bin/sh -c'echo Testing'
.因此,您可以期待某些差异,因为两个外壳都不同.
Then it is equivalent to running /bin/sh -c 'echo Testing'
. So you can expect certain differences as both the shells are different.
在Docker 1.12或更高版本中,您有一个名为 SHELL
的Dockerfile指令,这使您可以覆盖默认的 SHELL
In Docker 1.12 or higher you have a Dockerfile directive named SHELL
this allows you to override the default SHELL
SHELL ["/bin/bash", "-c"]
RUN echo Testing
这将使 RUN
命令作为 bash -c'echo Testing'
执行.您可以在此处了解有关 SHELL
指令的更多信息.>
This would make the RUN
command be executed as bash -c 'echo Testing'
. You can learn more about the SHELL
directive here
这篇关于如何完全像在Dockerfile中一样运行Docker cmds的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!