Docker runs a shell script in the background without leaving the container

I am trying to run a shell script in a docker container. The problem is that the shell of the script spawns another process, and it should continue to work if another shutdown of the script is not used to terminate the processes that spawn when the script starts.

When I ran the command below,

docker run image:tag /bin/sh /root/my_script.sh 

and then

 docker ps -a 

I see the team is out. But that I do not want. My question is: how to run a command in the background without exiting?

+7
linux docker
source share
4 answers

You did not explain why you want your container to run after the exit of your script, or you expect your script to complete.

The docker container exits as soon as the CMD container completes. If you want your container to continue to work, you will need a process that will work. One option is to simply put a while loop at the end of your script:

 while :; do sleep 300 done 

Your script will never exit, so your container will work. If your container has a network service (web server, database server, etc.), then this is usually a process that runs for the life of the container.

If instead your script comes out unexpectedly, you probably need to look at the logs of your container ( docker logs <container> ) and possibly add some debugging to the script.

If you just ask: “How do I run a container in the background?”, Then the answer from Emil (passing the -d flag to docker run ) will help you.

+3
source share

The process executed by docker replaces init in the UNIX process tree. init is the topmost parent process, and as soon as it exits the docker container. Any child process (now an orphan process ) will also be stopped.

 $ docker pull busybox >/dev/null $ time docker run --rm busybox sleep 3 real 0m3.852s user 0m0.179s sys 0m0.012s 

Thus, you cannot let the parent pid exit, but you have two options. You can leave the parent process in place and let it manage its children (for example, telling it to wait until all child processes have exited)

 $ time docker run --rm busybox sh -c 'sleep 3 & wait' real 0m3.916s user 0m0.178s sys 0m0.013s 

... or you can replace the parent process with a child process using exec . This means that the new command is executed in the parent space of the process ...

 $ time docker run --rm busybox sh -c 'exec sleep 3' real 0m3.886s user 0m0.173s sys 0m0.010s 

This latter approach may be complicated depending on the nature of the child process, but fewer unnecessary processes work more idiomatically by Docker. (Which does not say that you should have only one process.)

+4
source share

Wrap the program with docker -entrypoint.sh bash script, which blocks the container process and can catch ctrl-c. This bash example should help: https://rimuhosting.com/knowledgebase/linux/misc/trapping-ctrl-c-in-bash

The script should complete the process when the exit signal is sent to Docker.

You can also add a loop inside a script that repeatedly checks the running process.

+1
source share

Run the container using a script in the background using the command

 docker run -i -t -d image:tag /bin/sh /root/my_script.sh 

Check container ID with docker ps

Then make sure your script is running or not in the container

 docker exec <id> /bin/sh -l -c "ps aux" 
+1
source share

All Articles