Dockerfile CMD指令将在运行容器后立即退出容器 [英] Dockerfile CMD instruction will exit the container just after running it

查看:106
本文介绍了Dockerfile CMD指令将在运行容器后立即退出容器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在容器启动时设置一些配置,为此,我正在使用shell脚本.但是,一旦脚本结束,我的容器就会退出,我曾尝试使用-d标志/分离模式,但它永远不会在分离模式下运行.

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.

下面是我的Dockerfile

Below is my 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

下面是我的shell脚本

Below is my shell script

#!/bin/bash
echo Hello-docker

  1. 运行时没有任何标志

  1. Run without any flag

docker run hello-docker

这将在我的控制台上打印'Hello-docker'并退出

This will print 'Hello-docker' on my console and exits

使用-itd标志运行

docker run -itd hello-docker

,并在我的控制台输出下方,这一次也将很快退出. :(

and as below my console output, This time also will exits soon. :(

  • 当我运行其他图像时,我看到的区别是在COMMAND部分中,命令部分将显示"/bin/bash",并在分离模式下继续.

  • 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.

当我使用shell脚本在容器中运行映像时,COMMAND部分将显示"/bin/sh -c/usr/loca",然后退出.

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.

在Dockerfile中添加ENTRYPOINT指令后,这将不会执行我的shell脚本:(

After adding ENTRYPOINT instruction in Dockerfile, this will not execute my shell script :(

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

根据docker文档此处

As per docker documentation here

使用替代参数运行容器时,CMD将被覆盖,因此,如果我使用以下某些参数运行docker image,将不会执行CMD指令. :(

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. :(

sudo docker run -it --entrypoint=/bin/bash <imagename>

推荐答案

最后通过一些实验,我得到了如下最佳结果

Finally with some experiments I got my best result as below

我的Dockerfile没什么问题,如下所示.

There is nothing wrong with my Dockerfile as below it's correct.

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

要获得预期的结果,我只是在外壳脚本文件中添加了一个命令(/bin/bash),如下所示,一切都以我最好的方式进行.

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.

#!/bin/bash

echo "Hello-docker" > /usr/hello.txt

/bin/bash

这篇关于Dockerfile CMD指令将在运行容器后立即退出容器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆