0

I have a simple question hopefully; I'm new to Docker and Linux. Most of the articles/stackoverflow posts suggest installing cron INSIDE the docker container to get it to work as can be seen at this link

However, based on the picture attached below, we can see that the Docker Engine is an abstraction layer between the HOST OS's system and utility libraries and the application container.

Why are we not REUSING the system cron that comes with the HOST instead of installing cron INSIDE the container? It almost feels redundant.

My understanding of docker is you'd install application level libraries and packages like npm node modules INSIDE your nodejs app container for example but if you need a system utility like cron, then you would somehow call back out to the HOST OS's native cron utility; so why not use the HOST's cron within our container somehow, why reinstall cron inside the container?

enter image description here

Lastly, would you use docker-compose instead and separate out the cron service into its own container then somehow have the cron service talk to the application container and referencing its environment vars?

I mean the environment variables defined in the app container; making those accessible to the cron container? So that we may follow best practice of one service per container?

pelican
  • 5,846
  • 9
  • 43
  • 67
  • Stack Overflow is a site for programming and development questions. This question appears to be off-topic because it is not about programming or development. See [What topics can I ask about here](http://stackoverflow.com/help/on-topic) in the Help Center. Perhaps [Super User](http://superuser.com/) or [Unix & Linux Stack Exchange](http://unix.stackexchange.com/) would be a better place to ask. – jww Jul 19 '18 at 17:31

1 Answers1

1

Use cron on the host machine if you want, eg

0 0 * * * /usr/local/bin/docker run image 

As far as I'm aware, modern container applications use some form of scheduling from the host (eco)system. You could use cron on the host machine to trigger docker run commands. You could use a general purpose scheduler like Airflow. You could use a full-fletched container platform like DC/OS, which come with built in scheduling services.

There is nothing wrong with running cron together with your application inside a container per se. However, if you trigger the application container from a scheduling service outside your application container, your container would terminate after the job is finished, thus releasing any resources to other applications.

Secondly, it's considered good practice to have one container per service. Cron is a service in itself.

Tom Rijntjes
  • 614
  • 4
  • 16
  • Thanks Tom, what do you mean by There is nothing wrong with running cron inside a container per se, although it means you will often have running but idle containers? And lastly, by one container per service, you mean, decouple my application from the cron piece? Will that cron container know to communicate back to the app container bc the cron watches for some critical perl services to start in our app container and if they don't start, the cron starts them so how would it start those app services from a separate container, i.e outside of the app container? – pelican Jul 19 '18 at 14:52
  • Also the root of my question is, why are we reinstalling cron within a container, why can't we re-use the cron that comes prebuilt with the linux HOST ? By reunse I mean, from inside the container, is there a way to call back out to the HOST's native cron so that we woulnd't need to install the cron inside the container – pelican Jul 19 '18 at 14:53
  • Updated to hopefully answer your questions better. – Tom Rijntjes Jul 19 '18 at 14:58
  • You're saying what would happen but not explaining why. I'd like to get a deeper understanding of this subject. Could you for example explain why the container would terminate after the job is finished? – pelican Jul 19 '18 at 15:01
  • Sure. What kind of periodic jobs would you want to trigger with cron? – Tom Rijntjes Jul 19 '18 at 15:14
  • `## ## App configuration file ## APP_CONFIG=/utils/config/app.config # – pelican Jul 19 '18 at 15:17
  • A docker container exits when its main process finishes by design. Why? The question is, I think, why would you keep a container running without process? – Tom Rijntjes Jul 19 '18 at 15:19
  • This perl script watches some critical processes and if those processes are NOT started then this keep_up.pl will start them automatically. So how would you achieve this from a separate container since we should do one container per service? That separate cron container will not have all these files from the app container. – pelican Jul 19 '18 at 15:20
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/176345/discussion-between-pelican-and-tom-rijntjes). – pelican Jul 19 '18 at 15:20