bash Dockerfile CMD 指令会在运行后立即退出容器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42218957/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Dockerfile CMD instruction will exit the container just after running it
提问by Anand Suthar
I want to setup some configuration when my container starts, for this I am using shell scripts. But my container will exits as soon as my scripts ends, I have tried with -d flag / detached mode but It will never run in detached mode.
我想在容器启动时设置一些配置,为此我使用 shell 脚本。但是我的容器会在脚本结束后立即退出,我尝试使用 -d 标志/分离模式,但它永远不会在分离模式下运行。
Below is my Dockerfile
下面是我的 Dockerfile
FROM ubuntu:14.04
ADD shell.sh /usr/local/bin/shell.sh
RUN chmod 777 /usr/local/bin/shell.sh
CMD /usr/local/bin/shell.sh
Below is my shell script
下面是我的shell脚本
#!/bin/bash
echo Hello-docker
Run without any flag
docker run hello-docker
This will print 'Hello-docker' on my console and exits
Run with -itd flags
docker run -itd hello-docker
and as below my console output, This time also will exits soon. :(
The difference I saw is in COMMAND section when I run other images command section will shows "/bin/bash" and will continue in detached mode.
And when I run my image in container with shell script COMMAND section will show "/bin/sh -c /usr/loca", and Exit.
I want to run container till I not stop it manually.
不带任何标志运行
docker run hello-docker
这将在我的控制台上打印“Hello-docker”并退出
使用 -itd 标志运行
docker run -itd hello-docker
我在 COMMAND 部分看到的不同之处在于,当我运行其他图像时,命令部分将显示“/bin/bash”,并将在分离模式下继续。
当我在带有 shell 脚本命令部分的容器中运行我的图像时,将显示“/bin/sh -c /usr/loca”,然后退出。
我想运行容器,直到我不手动停止它。
EDIT:
编辑:
After adding ENTRYPOINT instruction in Dockerfile, this will not execute my shell script :(
在 Dockerfile 中添加 ENTRYPOINT 指令后,这将不会执行我的 shell 脚本 :(
FROM ubuntu:14.04
ADD shell.sh /usr/local/bin/shell.sh
RUN chmod 777 /usr/local/bin/shell.sh
CMD /usr/local/bin/shell.sh
ENTRYPOINT /bin/bash
As per docker documentation here
根据此处的docker 文档
CMD will be overridden when running the container with alternative arguments, so If I run docker image with some arguments as below, will not execute CMD instructions. :(
使用替代参数运行容器时,CMD 将被覆盖,因此如果我使用以下某些参数运行 docker image,将不会执行 CMD 指令。:(
sudo docker run -it --entrypoint=/bin/bash <imagename>
采纳答案by Anand Suthar
Finally with some experiments I got my best result as below
最后通过一些实验,我得到了最好的结果如下
There is nothing wrong with my Dockerfile as below it's correct.
我的 Dockerfile 没有任何问题,如下所示是正确的。
FROM ubuntu:14.04
ADD shell.sh /usr/local/bin/shell.sh
RUN chmod 777 /usr/local/bin/shell.sh
CMD /usr/local/bin/shell.sh
What I do to get expected result is, I just add one more command(/bin/bash) in my shell script file as below and vola everything works in my best way.
我为获得预期结果所做的是,我只是在我的 shell 脚本文件中再添加一个命令(/bin/bash),如下所示,vola 一切都以我最好的方式工作。
#!/bin/bash
echo “Hello-docker” > /usr/hello.txt
/bin/bash
回答by NZD
A docker container will run as long as the CMD
from your Dockerfile takes.
只要CMD
您的 Dockerfile 需要,docker容器就会运行。
In your case your CMD
consists of a shell script containing a single echo. So the container will exit after completing the echo.
在您的情况下,您CMD
由一个包含单个回声的 shell 脚本组成。所以容器会在完成 echo 后退出。
You can override CMD
, for example:
您可以覆盖CMD
,例如:
sudo docker run -it --entrypoint=/bin/bash <imagename>
This will start an interactive shell in your container instead of executing your CMD
. Your container will exit as soon as you exit that shell.
这将在您的容器中启动一个交互式 shell,而不是执行您的CMD
. 一旦您退出该外壳,您的容器就会退出。
If you want your container to remain active, you have to ensure that your CMD
keeps running. For instance, by adding the line while true; do sleep 1; done
to your shell.sh file, your container will print your hello message and then do nothing any more until you stop it (using docker stop in another terminal).
如果您希望容器保持活动状态,则必须确保您的容器CMD
继续运行。例如,通过while true; do sleep 1; done
将这一行添加到您的 shell.sh 文件中,您的容器将打印您的 hello 消息,然后在您停止它之前不再执行任何操作(在另一个终端中使用 docker stop)。
You can open a shell in the running container using docker exec -it <containername> bash
. If you then execute command ps ax
, it will show you that your shell.sh is still running inside the container.
您可以使用docker exec -it <containername> bash
. 如果您随后执行 command ps ax
,它将显示您的 shell.sh 仍在容器内运行。
回答by user2915097
You can also modify your first Dockerfile, replacing
你也可以修改你的第一个 Dockerfile,替换
CMD /usr/local/bin/shell.sh
CMD /usr/local/bin/shell.sh
by
经过
CMD /usr/local/bin/shell.sh ; sleep infinity
CMD /usr/local/bin/shell.sh ; sleep infinity
That way, your script does not terminate, and your container stays running.
这样,您的脚本不会终止,并且您的容器会保持运行。
回答by lanni654321
CMD bash -C '/path/to/start.sh';'bash'
回答by ghostyusheng
At your start shell append a line code:
tail -f /dev/null
or /bin/bash
to make sure you shell done and suspend a process in system so that docker container not shutdown.Don't forget to give "chmod +x" access to start.sh.
there is demo:
在您的启动 shell 中附加一行代码:
tail -f /dev/null
或者/bin/bash
确保您完成 shell 并挂起系统中的进程,以便 docker 容器不会关闭。不要忘记授予“chmod +x”对 start.sh 的访问权限。有演示:
#!/bin/bash
cp /root/supervisor/${RUN_SERVICE}.ini /etc/supervisor/conf.d/
sleep 1
service supervisor start
/bin/bash