docker container started in Detached mode stopped

2019-06-23 17:52发布

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"

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 ?

标签: linux docker
2条回答
smile是对你的礼貌
2楼-- · 2019-06-23 18:41

A Docker container will exit when its main process ends. In this case, that means when init.sh ends. 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. Using exec will effectively turn the called service/application into the main process.

If you have more than one service to start, you are best off using a process manager such as supervisord or runit. You will need to start the process manager daemon in the foreground. The Docker documentation includes an example of using supervisord.

查看更多
爱情/是我丢掉的垃圾
3楼-- · 2019-06-23 18:45

There are 2 modes of running docker container

  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

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

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

Then you can bash in to running container like this:

docker exec -it name /bin/bash -l

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 - You can create any sh script such as /entrypoint.sh. in entrypoint.sh you can run any never ending script as well

#!/bin/sh

#/entrypoint.sh

service mysql restart

...

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

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

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

查看更多
登录 后发表回答