When I start the cron service within the running Docker container with
service cron start
it works. But the line
RUN service cron start
within the Dockerfile seems not to have an effect. Why?
I thought about simply changing my CMD
to something like CMD ["startup.sh"]
which first runs service cron start
and then run.py
(a Python webserver). But I want the web server not to run as root and thus service cron start
would fail.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With