I want to keep a docker container running even after executing the run command (containers exit immediately after docker run..
. I know the command:
while :;do
sleep 300
done
during docker run
will make it run but how do I edit the Dockerfile itself in order to keep it running?
You can do this by putting the commands you want to execute into a script, and setting the script to be the command Docker runs when it starts a container:
When you build an image from this Dockerfile and run a container from the image, it will start
ping
in the background andsleep
in the foreground, so you can daemonize the container withdocker run -d
and it will keep running.This is not ideal though - Docker only monitors the last process it started when it ran the container, so it will be checking on
sleep
rather thanping
. If theping
command errors the container will keep running. Typically, you want the real application to be the only thing you start in theCMD
.