linux - docker container started in Detached mode stopped after process execution -


i create docker container in detached mode following command:

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

so need "/opt/init.sh" executed @ container created. saw container stopped after scripts finish executed.

how keep container started in detached script/services execution @ container creation ?

there 2 modes of running docker container

  1. detached mode - mode execute command , terminate container after command done
  2. foreground mode - mode run bash shell, terminate container after exit shell

what need background mode. not given in parameters there many ways this.

  1. run infinite command in detached mode command never ends , container never stops. use "tail -f /dev/null" because quite light weight , /dev/null present in linux images

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

then can bash in running container this:

docker exec -it name /bin/bash -l

if use -l parameter, login login mode execute .bashrc normal bash login. otherwise, need bash again inside manually

  1. entrypoint - can create sh script such /entrypoint.sh. in entrypoint.sh can run never ending script well

#!/bin/sh

#/entrypoint.sh

service mysql restart

...

tail -f /dev/null <- never ending

after save entrypoint.sh, chmod a+x on it, exit docker bash, start this:

docker run --name=name container --entrypoint /entrypoint.sh

this allows each container have own start script , can run them without worrying attaching start script each time


Comments

Popular posts from this blog

node.js - Mongoose: Cast to ObjectId failed for value on newly created object after setting the value -

[C++][SFML 2.2] Strange Performance Issues - Moving Mouse Lowers CPU Usage -

ios - Possible to get UIButton sizeThatFits to work? -