Docker and Cron

August 4, 2020

Lang: cs en de es

What prompted me to write this article was my experience with projects that use Docker, but not as they should. Are you using Docker and want to run regular jobs using Cron? In this article I will describe how to run Cron in a Docker container.

For example, I got to a project where the project didn't run after a server restart, and among other problems, cron didn't run. Or another project where Cron was running non-functional tasks, but nobody knew about it.

If you are just getting started with Docker, if you are then check out the How to get started with Docker article.

Docker and Services

Docker is container virtualization. And when you use it, you are also self-documenting the project. To leverage the power and benefits of container virtualization: oversight, independence, customizability, horizontal scaling, you need only one main program (service) running in each container. A program can have multiple threads or processes. For example, you can log into a container once and run other programs, but the main program is monitored to see if it is running.
Then, by running identical containers and application, you can horizontally scale and achieve greater performance of the application.
So a situation where there is another service (for example cron) running in the container besides the main program is wrong.

Docker image

The prebuilt base image runs just one service. Yes, it is possible to modify the image to automatically start other programs (e.g. cron) besides the main one, but if the secondary program is not running the runtime environment (docker, docker swarm, kubernetes) will not know about the problem, so nor you unless you modify the application or container otherwise.
But mainly, this is how the runtime environment monitors containers running - for example Kubernetes. Also, a given solution design is unlikely to allow you to scale without further modifications.
Yes there will be usecases where it is appropriate to run cron in the same container, but that will be a case dependent on specifics of the application/project.

What is Cron

By Cron, people understand the regular execution of programs or write configurations typical of Cron. Implementation-wise, a Cron is a program that runs automatically on startup on unix systems, constantly running and then executes tasks periodically according to the configuration specified.

How do I run cron jobs in docker?

So, to run cron in Docker, you need to have Cron installed, running and configure it.

When running cron in docker, we can start from any image either basic or already the necessary components. You install cron into that image and configure the docker image to run cron.

Next, replace the /etc/crontab file with your own configuration for cron.

The Dockerfile for cron, based on the base Debian image, might look like this:

FROM debian:buster
MAINTAINER Bc. Josef Jebavý

RUN echo 'debconf debconf/frontend select Noninteractive' | debconf-set-selections \
 && apt-get update && apt install cron curl wget jq -y && apt-get clean

# other components

COPY crontab /etc/crontab
CMD [ "/usr/sbin/cron","-f","-L /dev/stdout" ]

The result is an image from which you start a container that runs a cron that is independent of the others services/applications. When cron crashes, you will be notified of the crash and typically in kubernetes the container will be restarted automatically.
The resulting grouped application components will then be that many separate containers can run different parts of the application, some multiple times, and one single separate container running cron alongside.

You can also mount the configuration file for cron when you start the container. This is useful, for example, if you need to debug the configuration on a development environment, because then you don't have to constantly build and start a new container.

Example cron file:

# crontab copy to docker

# m h dom mon dow user command
17 * * * * root cd / && run-parts --report /etc/cron.hourly
25 6 * * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etcocron.daily )
47 6 * * 7 root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
52 6 1 * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )

* * * * * root date >> /var/www/html/log/cron-test.txt

1.31 * * * * root ( ${PHP5} /var/www/html/cron/10-update.php) >> /var/www/html/log/cron.log 2>&1

Custom solutions

Since a programmer is a software development expert, you should leave the preparation of containers and the design of a running production environment to Linux and Docker experts.

Articles on a similar topic

VMware vs Proxmox: performance comparison
GitLab CI/CD: test automation and application deployment
Migrating VPS from VMware to Proxmox
VMware licensing change
Running Microsoft SQL Server on Linux
Backup: the Proxmox Backup Server
Linux as a router and firewall
How to upload a docker image to the Docker Registry
Linux: logical volume management
Linux Software RAID
Running a web application behind a proxy
Mailbox migration
Docker multistage build
Backing up your data by turning on your computer
Importing Windows into Proxmox virtualization
Docker and PHP mail
Proxmox virtualization
Lenovo ThinkPad X1 Carbon: LTE modem EM7544 commissioning
Yocto Project: Build custom operating system for embedded devices
Preparing a Linux server to run a web application in Python
How to address poor file share performance in Docker
How to get started using Docker correctly
Installing Linux on a dedicated HPE ProLiant DL320e server
How to stress test a web application
Why use the JFS filesystem
How to boot from a 4TB drive with GTP using UEFI
Btrfs file system
Raspberry PI
WINE - running Windous programs under Linux
GNU/Linux operating system


If you are interested in receiving occasional news by email.
You can register by filling in your email news subscription.