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 ?
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 useexec
to launch it at the end, making sure to run it in the foreground. Usingexec
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
orrunit
. You will need to start the process manager daemon in the foreground. The Docker documentation includes an example of using supervisord.There are 2 modes of running docker container
What you need is Background mode. This is not given in parameters but there are many ways to do this.
Then you can bash in to running container like this:
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
After you save this entrypoint.sh, chmod a+x on it, exit docker bash, then start it like this:
This allows each container to have their own start script and you can run them without worrying about attaching the start script each time