-1

I am fairly new to docker and I am still trying to figure out what the best practices and conventions are.

One thing I see over and over again is: "each container should be responsible for just a single process".

I am coming from the average LAMP stack. So one apache server is serving multiple websites running on php, and they all have some cronjobs running (combined in a single cron-file).

So, the docker way... A single container for mysql. A single container for php+apache (can access the mysql container via the 'docker-network'.

But now the cronjobs. I see people saying: you should make a seperate container for that. But, to my knowning, there is no way the cron-container would be able to access the php-command from the php+apache container.

What is the convention here? I feel like I should put the cron in the php+apache container, and just run it as an additional process in that container. Is that correct?

Next to this: should every website have its own docker php+apache+cron container? Or is it 'allowed' to bundle them together?

Any advice? Thanks!

Xriter
  • 160
  • 1
  • 1
  • 8
  • You should separate it. If the PHP container dies, all cronjobs are dead. – Markus Zeller Aug 30 '22 at 16:41
  • But *why* should it be able to access the php-command from the php+apache container? Why can't it access its own php? – Your Common Sense Aug 30 '22 at 16:43
  • @MarkusZeller The cronjobs will execute some php scripts. So it kind of makes sense if the php container dies the cronjobs can't be executed. – Xriter Aug 30 '22 at 17:05
  • @YourCommonSense In that case I would have cron+php container next to my php+apache container? What benefit does this have then over having a php+apache+cron container? – Xriter Aug 30 '22 at 17:06
  • 1
    Then ask yourself why did you move mysql container away first. It uses the same Debian/Ubuntu system as PHP-Apache container does. Why cannot mysql use the same system then? How having same piece of software in two containers can justify anything? – Your Common Sense Aug 30 '22 at 17:12
  • @YourCommonSense Haha that is a very good question. I am trying to figure out what the conventions and best practices are, but coming from a 'simple' LAMP setup, it's still hard sometimes to rewire my brain to what 'the docker way' is. – Xriter Aug 30 '22 at 17:17
  • When you have apache+php, and a cron container, you could even trigger a php job via http call. – Markus Zeller Aug 30 '22 at 17:35
  • https://stackoverflow.com/questions/59035543/how-to-execute-command-from-one-docker-container-to-another – Markus Zeller Aug 30 '22 at 17:39
  • Consider installing cron in the container _image_ and running a second container with that same image, but invoking crond as the command instead. – Sammitch Aug 30 '22 at 18:45

1 Answers1

0

There is no right or wrong answer here, it really depends on your specific needs and preferences. If you want to keep your containers small and focused on a single task, then you would want to create a separate container for your cronjobs. However, if you don't mind having a larger container that handles multiple tasks, then you can definitely put your cronjobs in the same container as your PHP + Apache server.