使用主机的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?

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.