How to run a cron job inside a docker container?

前端 未结 20 1290
無奈伤痛
無奈伤痛 2020-11-22 05:36

I am trying to run a cronjob inside a docker container that invokes a shell script.

Yesterday I have been searching all over the web and stack overflow, but I could

20条回答
  •  名媛妹妹
    2020-11-22 06:10

    The adopted solution may be dangerous in a production environment.

    In docker you should only execute one process per container because if you don't, the process that forked and went background is not monitored and may stop without you knowing it.

    When you use CMD cron && tail -f /var/log/cron.log the cron process basically fork in order to execute cron in background, the main process exits and let you execute tailf in foreground. The background cron process could stop or fail you won't notice, your container will still run silently and your orchestration tool will not restart it.

    You can avoid such a thing by redirecting directly the cron's commands output into your docker stdout and stderr which are located respectively in /proc/1/fd/1 and /proc/1/fd/2.

    Using basic shell redirects you may want to do something like this :

    * * * * * root echo hello > /proc/1/fd/1 2>/proc/1/fd/2
    

    And your CMD will be : CMD ["cron", "-f"]

提交回复
热议问题