Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

Alternatives

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.

like image 756
Martin Thoma Avatar asked Sep 15 '17 09:09

Martin Thoma


2 Answers

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.

like image 155
BMitch Avatar answered Oct 25 '22 22:10

BMitch


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

like image 25
Bukharov Sergey Avatar answered Oct 25 '22 22:10

Bukharov Sergey