在进程执行后以分离模式启动的Docker容器停止 [英] docker container started in Detached mode stopped after process execution

查看:113
本文介绍了在进程执行后以分离模式启动的Docker容器停止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下命令以分离模式创建我的docker容器:

I create my docker container in detached mode with the following command:

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

所以我需要在创建的容器上执行"/opt/init.sh".我看到容器在脚本执行完后停止了.

so I need that "/opt/init.sh" executed at container created. 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 ?

推荐答案

有两种运行docker容器的模式

There are 2 modes of running docker container

  1. 独立模式-这种模式下,您将执行命令,并在执行完命令后终止容器
  2. 前景模式-此模式运行bash shell,但在退出shell后也会终止容器

您需要的是背景模式.参数中没有给出,但是有很多方法可以做到这一点.

What you need is Background mode. This is not given in parameters but there are many ways to do this.

  1. 在分离模式下运行无限命令,因此该命令永无止境,并且容器永不停止.我通常使用"tail -f/dev/null"只是因为它的重量很轻并且大多数Linux映像中都存在/dev/null

docker run -d --name = name容器尾-f/dev/null

docker run -d --name=name container tail -f /dev/null

然后您可以像这样扑向正在运行的容器:

Then you can bash in to running container like this:

docker exec -it名称/bin/bash -l <​​/p>

docker exec -it name /bin/bash -l

如果使用-l参数,它将以登录方式登录,该方式将像普通的bash登录一样执行.bashrc.否则,您需要在内部手动重击

If you use -l parameter, it will login as login mode which will execute .bashrc like normal bash login. Otherwise, you need to bash again inside manually

  1. Entrypoint-您可以创建任何sh脚本,例如/entrypoint.sh.在entrypoint.sh中,您也可以运行任何永无止境的脚本

#!/bin/sh

#!/bin/sh

#/entrypoint.sh

#/entrypoint.sh

服务mysql重新启动

service mysql restart

...

tail -f/dev/null<-这永远不会结束

tail -f /dev/null <- this is never ending

保存此entrypoint.sh后,在其上使用chmod a + x,退出docker bash,然后按以下方式启动它:

After you save this entrypoint.sh, chmod a+x on it, exit docker bash, then start it like this:

docker run --name = name容器--entrypoint/entrypoint.sh

docker run --name=name container --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

这篇关于在进程执行后以分离模式启动的Docker容器停止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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