Zaher Ghaibeh
PHP Backend developer
I've experience in a few PHP Frameworks, such as Laravel, Lumen and Slim (The last two are used for building Microservices/API services).
Run crontab within Alpinelinux Docker image
Published at Sunday, December 3, 2017 , Categorized under: Code, docker, Linux

This post will explain how we can run cronjobs within Docker image based on Alpine Linux distribution, so am going to use my newest PHP 7.2 docker image, which you can find at zaherg/php-7.2-xdebug-alpine .

First, we will have to pull the docker image using the command:

docker pull zaherg/php-7.2-xdebug-alpine

Now that we have downloaded the image, we will have to familiarize our self with the structure for the periodic directory which we can find under /etc/periodic, this directory should contain the following folders:

  1. 15min
  2. hourly
  3. daily
  4. weekly
  5. monthly

Your scripts should be put within the folder that you think is the best for you, but you need to make sure that your scripts follow these two rules:

  1. start with #!/bin/sh
  2. that your script is executable chmod a+x

Let's start playing with my docker image, but since it runs with the user `www-data` who do not have write permission on those directories, so we will have to run the image with the root user using the following command

docker run --rm -it --user root zaherg/php-7.2-xdebug-alpine:latest sh

once we are inside the docker image we can go to one of the directories listed above, or even create our own if we like, and create a simple script which will just `echo hello world`

# cd /etc/periodic
/etc/periodic/ # mkdir everymin
/etc/periodic/ # cd everymin
/etc/periodic/everymin # cat > hello << EOF
> #!/bin/sh
>
> echo "Hello world"  >> /var/log/cron.log 2>&1
> EOF
/etc/periodic/everymin # chmod a+x hello
/etc/periodic/everymin # ls -al
total 12
drwxr-xr-x    2 root     root          4096 Dec  3 12:20 .
drwxr-xr-x    1 root     root          4096 Dec  3 12:00 ..
-rwxr-xr-x    1 root     root            57 Dec  3 12:01 hello

As we can see I created a directory called everymin , and then created a file within the everymin  directory and called it hello (no suffix or extension), and remember to make sure that your script is executable. Now we will see how to add our script to the cronjob list, as we have mentioned before alpine use crond from busybox so a small search you will see that the file which crond reads is located at /etc/crontabs/root :

#  cd /etc/crontabs/
/etc/crontabs # ls -al
total 12
drwxr-xr-x    1 root     root          4096 Dec  1 16:31 .
drwxr-xr-x    1 root     root          4096 Dec  3 12:00 ..
-rw-------    1 root     root           355 Dec  3 12:01 root

we need to add our everyday directory to the root file so we can sure it will be executed

# min   hour    day     month   weekday command
*/15    *       *       *       *       run-parts /etc/periodic/15min
0       *       *       *       *       run-parts /etc/periodic/hourly
0       2       *       *       *       run-parts /etc/periodic/daily
0       3       *       *       6       run-parts /etc/periodic/weekly
0       5       1       *       *       run-parts /etc/periodic/monthly
*       *       *       *       *       run-parts /etc/periodic/everymin

and remember you will have to use vi not nano ?.

To run the scheduler all we need to do is to execute the command crond -f as from the help -f means in the foreground

/ # crond --help
BusyBox v1.24.2 (2016-06-22 17:51:28 GMT) multi-call binary.

Usage: crond -fbS -l N -d N -L LOGFILE -c DIR

    -f  Foreground
    -b  Background (default)
    -S  Log to syslog (default)
    -l N    Set log level. Most verbose:0, default:8
    -d N    Set log level, log to stderr
    -L FILE Log to FILE
    -c DIR  Cron dir. Default:/var/spool/cron/crontabs

now you will get something like the following image

Now that you know how to deal with crond in Alpine Linux, you can build your own Docker image that use this information to run some scheduled tasks.

PS: if you know a better way please share your idea, so we can all benefit from it.