[英]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容器的模式
What you need is Background mode. 您需要的是背景模式。 This is not given in parameters but there are many ways to do this.
这在参数中没有给出,但是有很多方法可以做到这一点。
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
否则,您需要手动重新进行内部打击
#!/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
. 如果您要启动一项以上的服务,则最好使用流程管理器(例如
supervisord
或runit
。 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.