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
- detached mode - mode execute command , terminate container after command done
- foreground mode - mode run bash shell, terminate container after exit shell
what need background mode. not given in parameters there many ways this.
- 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
- 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
Post a Comment