Docker - start a container at random time

Viewed 152

I have 3 vps with docker installed. I have created a docker image and I would like to know if there is a possibility to configure docker or docker-compose in such a way that these 3 containers run only once a day at random hours.

I have not found any way other than to configure the command to start the docker in a bash script, called from the Linux crontab, which waits for a while at random. Are there better better solutions? Maybe k8s / k3s?

2 Answers

This is a community wiki answer posted for better visibility. Feel free to expand it.

Possible solutions to this problem are listed below.

Docker

Bash script, which is called from Linux crontab to start Docker.

Kubernetes

Since Docker / Docker-compose does't have its own way of scheduling a container to run at random time, CronJob might be the easiest way to do this.

More information about CronJobs is available in the official documentation

If "randomness" is part of your application behaviour, then you should contain this logic inside one of services / containers, so this is no-brainer. You are running entire stack with docker-compose and it just works.

Other than that Crontab or other external scheduler (like kubernetes cron jobs) is the way of doing that if running those three containers on random time once a day is your requirement of using those docker services.

Related