Reputation: 579
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 ?
Upvotes: 7
Views: 6025
Reputation: 973
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.
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
#!/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
Upvotes: 9
Reputation: 46528
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.
Upvotes: 1