Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

docker container started in Detached mode stopped after process execution

Tags:

linux

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"

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 ?

like image 393
Dumitru Gutu Avatar asked Mar 16 '15 21:03

Dumitru Gutu


1 Answers

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

like image 198
Grant Li Avatar answered Sep 19 '22 20:09

Grant Li