doutongxuan1614 2019-06-07 13:47
浏览 730

使用主机的crontab从Dockerfile定义crontab每小时运行一个容器化的Go程序

I have a pretty simple Go program that queries a database for rows with some inconsistency according to a business rule, then updates the rows so as to be consistent with the business rule. This is all fine, I can run the program as a binary and it just works. However, I'm also supposed to containerize this program with Docker and have it run every hour.

What I'm not sure of is the best way to get the program to run once an hour. What I'd like to do is use crontab, but I have no idea how to get Docker to interface with it (I am still a Docker noob, and yes, I have read the introductory docs, this is just my first time getting my hands dirty).

Another consideration is to do this within the Go program itself, but I'm not sure if that's an elegant or safe solution considering the requirements I've been given. I'd like for the program to be low-overhead, and that approach seems to imply writing it as a daemon, which seems like overkill.

So is it possible to containerize my simple program, and define the crontab within the Dockerfile somehow, utilizing the host system's crontab? If not, what is the correct approach to this problem?

  • 写回答

1条回答 默认 最新

  • duanchao5258 2019-06-07 13:52
    关注

    Defining crontab in Dockerfile or doing it in Go application means that you have to run your service permanently, so it will spend CPU/Memory while doing nothing.

    It's more efficient to separate jobs from the application, there can be multiple approaches to do so depends on what are you using:

    • If you use Kubernetes - create a Kubernetes Job that will run your container based on defined frequency.
    • Use standard crontab and define command as docker run ...
    • Use AWS Lambda / Google Cloud functions and trigger functions using specified the interval
    • other options.
    评论

报告相同问题?

悬赏问题

  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建