The cron command-line utility is a job scheduler on Unix-like operating systems. Users who set up and maintain software environments use cron to schedule jobs, also known as cron jobs, to run periodically at fixed times, dates, or intervals.
Docker is a set of platform as a service products that use OS-level virtualization to deliver software in packages called containers.
This blog describes how to implement crontab functionality in docker.
I will create 3 Linux shell scripts:
- the first one will be scheduled to run every 1 minute
- the second one will be scheduled to run every 2 minutes
- the third one will be scheduled to run every 3 minutes
Each script will generate some message. The output from the scripts is redirected to a log file. The log file will be printed when a docker container runs.
Step 1: Create a new directory
mkdir cron-docker
Step 2: Create the 3 Linux shell scripts
script_to_run_every_1min.sh:
dmi@dmi-VirtualBox:~/cron_docker$ cat script_to_run_every_1min.sh
#!/bin/bash
echo "$0: $(date)"
script_to_run_every_2min.sh:
dmi@dmi-VirtualBox:~/cron_docker$ cat script_to_run_every_2min.sh
#!/bin/bash
echo "$0: $(date)"
script_to_run_every_3min.sh:
dmi@dmi-VirtualBox:~/cron_docker$ cat script_to_run_every_3min.sh
#!/bin/bash
echo "$0: $(date)"
Step 3: Create cron_task file
dmi@dmi-VirtualBox:~/cron_docker$ cat cron_task
* * * * * root /my_cron_in_docker/script_to_run_every_1min.sh >> /my_cron_in_docker/cron_task.log 2>&1
*/2 * * * * root /my_cron_in_docker/script_to_run_every_2min.sh >> /my_cron_in_docker/cron_task.log 2>&1
*/3 * * * * root /my_cron_in_docker/script_to_run_every_3min.sh >> /my_cron_in_docker/cron_task.log 2>&1
# empty line
Step 4: Create Dockerfile
dmi@dmi-VirtualBox:~/cron_docker$ cat Dockerfile
FROM ubuntu:22.04
MAINTAINER Dmitry R
RUN apt update && apt install cron -y
RUN mkdir -p my_cron_in_docker
ADD script_to_run_every_1min.sh my_cron_in_docker/script_to_run_every_1min.sh
ADD script_to_run_every_2min.sh my_cron_in_docker/script_to_run_every_2min.sh
ADD script_to_run_every_3min.sh my_cron_in_docker/script_to_run_every_3min.sh
RUN chmod 0755 my_cron_in_docker/script_to_run_every_1min.sh
RUN chmod 0755 my_cron_in_docker/script_to_run_every_2min.sh
RUN chmod 0755 my_cron_in_docker/script_to_run_every_3min.sh
ADD cron_task /etc/cron.d/cron_task
RUN chmod 0644 /etc/cron.d/cron_task
RUN touch my_cron_in_docker/cron_task.log
CMD cron && tail -f my_cron_in_docker/cron_task.log
dmi@dmi-VirtualBox:~/cron_docker$
Step 5: Create Docker image from the Dockerfile
docker build -t mycron .
Step 6: Start container
docker run mycron
Step 7: Check that the implemented functionality works correctly
dmi@dmi-VirtualBox:~/cron_docker$ docker run mycron
/my_cron_in_docker/script_to_run_every_1min.sh: Fri Dec 2 14:20:01 UTC 2022
/my_cron_in_docker/script_to_run_every_2min.sh: Fri Dec 2 14:20:01 UTC 2022
/my_cron_in_docker/script_to_run_every_3min.sh: Fri Dec 2 14:21:01 UTC 2022
/my_cron_in_docker/script_to_run_every_1min.sh: Fri Dec 2 14:21:01 UTC 2022
/my_cron_in_docker/script_to_run_every_2min.sh: Fri Dec 2 14:22:01 UTC 2022
/my_cron_in_docker/script_to_run_every_1min.sh: Fri Dec 2 14:22:01 UTC 2022
/my_cron_in_docker/script_to_run_every_1min.sh: Fri Dec 2 14:23:01 UTC 2022
/my_cron_in_docker/script_to_run_every_1min.sh: Fri Dec 2 14:24:01 UTC 2022
/my_cron_in_docker/script_to_run_every_3min.sh: Fri Dec 2 14:24:01 UTC 2022
/my_cron_in_docker/script_to_run_every_2min.sh: Fri Dec 2 14:24:01 UTC 2022
/my_cron_in_docker/script_to_run_every_1min.sh: Fri Dec 2 14:25:01 UTC 2022
/my_cron_in_docker/script_to_run_every_2min.sh: Fri Dec 2 14:26:02 UTC 2022
/my_cron_in_docker/script_to_run_every_1min.sh: Fri Dec 2 14:26:02 UTC 2022
/my_cron_in_docker/script_to_run_every_1min.sh: Fri Dec 2 14:27:01 UTC 2022
/my_cron_in_docker/script_to_run_every_3min.sh: Fri Dec 2 14:27:01 UTC 2022
/my_cron_in_docker/script_to_run_every_2min.sh: Fri Dec 2 14:28:01 UTC 2022
/my_cron_in_docker/script_to_run_every_1min.sh: Fri Dec 2 14:28:01 UTC 2022
/my_cron_in_docker/script_to_run_every_1min.sh: Fri Dec 2 14:29:01 UTC 2022
/my_cron_in_docker/script_to_run_every_3min.sh: Fri Dec 2 14:30:01 UTC 2022
/my_cron_in_docker/script_to_run_every_1min.sh: Fri Dec 2 14:30:01 UTC 2022
/my_cron_in_docker/script_to_run_every_2min.sh: Fri Dec 2 14:30:01 UTC 2022
/my_cron_in_docker/script_to_run_every_1min.sh: Fri Dec 2 14:31:01 UTC 2022
/my_cron_in_docker/script_to_run_every_2min.sh: Fri Dec 2 14:32:01 UTC 2022
/my_cron_in_docker/script_to_run_every_1min.sh: Fri Dec 2 14:32:01 UTC 2022
/my_cron_in_docker/script_to_run_every_3min.sh: Fri Dec 2 14:33:01 UTC 2022
/my_cron_in_docker/script_to_run_every_1min.sh: Fri Dec 2 14:33:01 UTC 2022
/my_cron_in_docker/script_to_run_every_1min.sh: Fri Dec 2 14:34:01 UTC 2022
/my_cron_in_docker/script_to_run_every_2min.sh: Fri Dec 2 14:34:01 UTC 2022
/my_cron_in_docker/script_to_run_every_1min.sh: Fri Dec 2 14:35:01 UTC 2022
/my_cron_in_docker/script_to_run_every_3min.sh: Fri Dec 2 14:36:01 UTC 2022
/my_cron_in_docker/script_to_run_every_1min.sh: Fri Dec 2 14:36:01 UTC 2022
/my_cron_in_docker/script_to_run_every_2min.sh: Fri Dec 2 14:36:01 UTC 2022
/my_cron_in_docker/script_to_run_every_1min.sh: Fri Dec 2 14:37:01 UTC 2022
/my_cron_in_docker/script_to_run_every_1min.sh: Fri Dec 2 14:38:01 UTC 2022
/my_cron_in_docker/script_to_run_every_2min.sh: Fri Dec 2 14:38:01 UTC 2022
/my_cron_in_docker/script_to_run_every_1min.sh: Fri Dec 2 14:39:02 UTC 2022
/my_cron_in_docker/script_to_run_every_3min.sh: Fri Dec 2 14:39:02 UTC 2022
/my_cron_in_docker/script_to_run_every_2min.sh: Fri Dec 2 14:40:01 UTC 2022
/my_cron_in_docker/script_to_run_every_1min.sh: Fri Dec 2 14:40:01 UTC 2022
/my_cron_in_docker/script_to_run_every_1min.sh: Fri Dec 2 14:41:01 UTC 2022
/my_cron_in_docker/script_to_run_every_3min.sh: Fri Dec 2 14:42:01 UTC 2022
/my_cron_in_docker/script_to_run_every_1min.sh: Fri Dec 2 14:42:01 UTC 2022
/my_cron_in_docker/script_to_run_every_2min.sh: Fri Dec 2 14:42:01 UTC 2022
/my_cron_in_docker/script_to_run_every_1min.sh: Fri Dec 2 14:43:01 UTC 2022
/my_cron_in_docker/script_to_run_every_2min.sh: Fri Dec 2 14:44:01 UTC 2022
/my_cron_in_docker/script_to_run_every_1min.sh: Fri Dec 2 14:44:01 UTC 2022
/my_cron_in_docker/script_to_run_every_1min.sh: Fri Dec 2 14:45:01 UTC 2022
Conclusion
In this blog post I’ve demonstrated how to implement cron jobs in docker. This approach can be used to create scheduler(s) in docker containers.
Top comments (0)