简体   繁体   English

在流程执行后以分离模式启动的Docker容器停止

[英]docker container started in Detached mode stopped after process execution

I create my docker container in detached mode with the following command: 我使用以下命令以分离模式创建我的Docker容器:

docker run [OPTIONS] --name="my_image" -d container_name /bin/bash -c "/opt/init.sh"

so I need that "/opt/init.sh" executed at container created. 所以我需要在创建的容器上执行“ /opt/init.sh”。 What I saw that the container is stopped after scripts finish executed. 我看到的是,脚本执行完后容器已停止。

How to keep container started in detached with script/services execution at container creation ? 如何在创建容器时通过脚本/服务执行来使容器保持启动状态?

There are 2 modes of running docker container 有两种运行docker容器的模式

  1. Detached mode - This mode you execute a command and will terminate container after the command is done 分离模式-此模式下您将执行命令,并在命令完成后终止容器
  2. Foreground mode - This mode you run a bash shell, but will also terminate container after you exit the shell 前景模式-此模式运行bash shell,但在退出shell后也会终止容器

What you need is Background mode. 您需要的是背景模式。 This is not given in parameters but there are many ways to do this. 这在参数中没有给出,但是有很多方法可以做到这一点。

  1. Run an infinite command in detached mode so the command never ends and the container never stops. 在分离模式下运行无限命令,因此该命令永不结束,并且容器永不停止。 I usually use "tail -f /dev/null" simply because it is quite light weight and /dev/null is present in most linux images 我通常使用“ tail -f / dev / null”只是因为它的重量很轻并且大多数Linux映像中都存在/ dev / null。

docker run -d --name=name container tail -f /dev/null docker run -d --name =名称容器尾巴-f / dev / null

Then you can bash in to running container like this: 然后,您可以像这样扑向正在运行的容器:

docker exec -it name /bin/bash -l docker exec -it名称/ bin / bash -l

If you use -l parameter, it will login as login mode which will execute .bashrc like normal bash login. 如果使用-l参数,它将以登录方式登录,该方式将像普通的bash登录一样执行.bashrc。 Otherwise, you need to bash again inside manually 否则,您需要手动重新进行内部打击

  1. Entrypoint - You can create any sh script such as /entrypoint.sh. 入口点-您可以创建任何sh脚本,例如/entrypoint.sh。 in entrypoint.sh you can run any never ending script as well 在entrypoint.sh中,您也可以运行任何永无止境的脚本

#!/bin/sh #!/ bin / sh

#/entrypoint.sh #/ entrypoint.sh

service mysql restart 服务mysql重启

... ...

tail -f /dev/null <- this is never ending tail -f / dev / null <-这永远不会结束

After you save this entrypoint.sh, chmod a+x on it, exit docker bash, then start it like this: 保存该entrypoint.sh后,在其上执行chmod a + x,退出docker bash,然后按以下方式启动它:

docker run --name=name container --entrypoint /entrypoint.sh docker运行--name = name容器--entrypoint /entrypoint.sh

This allows each container to have their own start script and you can run them without worrying about attaching the start script each time 这允许每个容器都有自己的启动脚本,您可以运行它们而不必担心每次都附加启动脚本

A Docker container will exit when its main process ends. Docker容器的主进程结束时将退出。 In this case, that means when init.sh ends. 在这种情况下,这意味着init.sh结束。 If you are only trying to start a single application, you can just use exec to launch it at the end, making sure to run it in the foreground. 如果仅尝试启动单个应用程序,则可以只在最后使用exec来启动它,并确保在前台运行它。 Using exec will effectively turn the called service/application into the main process. 使用exec将有效地将被调用的服务/应用程序转换为主过程。

If you have more than one service to start, you are best off using a process manager such as supervisord or runit . 如果您要启动一项以上的服务,则最好使用流程管理器(例如supervisordrunit You will need to start the process manager daemon in the foreground. 您将需要在前台启动流程管理器守护程序。 The Docker documentation includes an example of using supervisord . Docker文档包含一个使用超级用户的示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM