Why doesn't "service cron start" work within the Docker file? Why doesn't "service cron start" work within the Docker file? docker docker

Why doesn't "service cron start" work within the Docker file?


Each RUN command creates a temporary container on your build host using the resulting image from the prior step and the RUN arg as the container's command. A container only runs as long as the command you execute is active, so if it's a command that launches a background daemon like you've done, the container exits when the command returns, not when the background daemon exits.

The RUN command is used to build up your image which is the layered filesystem and various meta data that tells docker how to use that filesystem (environment variables, entrypoint and/or command to run by default, etc). It is very different from a CMD which tells docker what to run when that image is turned into a container. So for a process that you need to have running in the container, that needs to be part of your CMD or ENTRYPOINT since running processes from RUN are not part of a static filesystem image.

I'd also follow the advice of others and copy an already existing image with cron inside, no need to reinvent this wheel. You may want to use a tool like supervisord to run cron and your application together if you need to have multiple processes inside your container. Though when possible, you should work out how to break this into multiple containers that can be independently updated.


When you execute command service cron start process cron will be detached from shell. But Docker works only while main process is alive.

You need move cron into entrypoint. A good example here