简体   繁体   English

在docker云容器中运行cronjob或任务

[英]Running a cronjob or task inside a docker cloud container

I got stuck and need help. 我被困住了,需要帮助。 I have setup multiple stacks on docker cloud. 我已经在docker cloud上设置了多个堆栈。 The stacks are running multiple container like data, mysql, web, elasticsearch, etc. 堆栈正在运行多个容器,例如数据,mysql,web,elasticsearch等。

Now I need to run commands on the web containers. 现在,我需要在Web容器上运行命令。 Before docker I did this with cronjob eg: 在码头工人之前,我用cronjob做到了这一点,例如:

*/10 * * * * php /var/www/public/index.php run my job

But my web Dockerfile ends with 但是我的网络Dockerfile以

CMD ["apache2-foreground"]

As I understand the docker concept running two commands on one container would be bad practice. 据我了解,docker概念在一个容器上运行两个命令将是一种不好的做法。 But how would I schedule a job like the one cronjob above? 但是我该如何安排像上面的一个cronjob这样的工作?

Should I start cron in the CMD too, something like? 我也应该在CMD中启动cron吗?

CMD ["cron", "apache2-foreground"] ( should exit with 0 before apache starts)

Should I make a start up script running both commands? 我应该制作一个运行两个命令的启动脚本吗?

In my opinion the smartest solution would be to create another service like the dockercloud haproxy one, where other services are linked. 在我看来,最聪明的解决方案是创建另一个服务,例如dockercloud haproxy,将其他服务链接在一起。

Then the cron service would exec commands that are defined in the Stackfile of the linked containers/stacks. 然后,cron服务将执行在链接的容器/堆栈的Stackfile中定义的命令。

Thanks for your help 谢谢你的帮助

With docker in general I see 3 options: 一般而言,使用docker我会看到3个选项:

  • run your cron process in the same container 在同一容器中运行您的cron进程
  • run your cron process in a different container 在另一个容器中运行您的cron进程
  • run cron on the host, outside of docker 在docker之外的主机上运行cron

For running cron in the same container you can look into https://github.com/phusion/baseimage-docker 要在同一容器中运行cron,可以查看https://github.com/phusion/baseimage-docker

Or you can create a separate container where the only running process inside is the cron daemon. 或者,您可以创建一个单独的容器,其中唯一正在运行的进程是cron守护程序。 I don't have a link handy for this, but they are our there. 我没有方便的链接,但是它们在那里。 Then you use the cron invocations to connect to the other containers and call what you want to run. 然后,使用cron调用连接到其他容器,并调用要运行的容器。 With an apache container that should be easy enough, just expose some minimal http API endpoint that will do what you want done when it's called (make sure it's not vulnerable to any injections, ie don't pass any arguments, keep it simple stupid). 使用应该足够简单的apache容器,只需公开一些最小的http API端点即可在调用时完成您想要的操作(确保它不容易受到任何注入攻击,即不传递任何参数,保持其简单愚蠢) 。

If you have control of the host as well then you can (ab)use the cron daemon running there (I currently do this with my containers). 如果您还可以控制主机,则可以(ab)使用在那里运行的cron守护程序(我目前使用容器执行此操作)。 I don't know docker cloud, but something tells me that this might not be an option for you. 我不知道docker cloud,但是有些事情告诉我,这可能不是您的选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM