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:

Shell

1

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:

15min

hourly

daily

weekly

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:

start with #!/bin/sh

that your script is executable chmod a+x <scriptfile>

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

PHP

1

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

Shell

1

2

3

4

5

6

7

8

9

10

11

12

13

14

# 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.log2>&1

>EOF

/etc/periodic/everymin# chmod a+x hello

/etc/periodic/everymin# ls -al

total12

drwxr-xr-x2root root4096Dec312:20.

drwxr-xr-x1root root4096Dec312:00..

-rwxr-xr-x1root root57Dec312:01hello

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 :

Shell

1

2

3

4

5

6

# cd /etc/crontabs/

/etc/crontabs# ls -al

total12

drwxr-xr-x1root root4096Dec116:31.

drwxr-xr-x1root root4096Dec312:00..

-rw-------1root root355Dec312:01root

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

Shell

1

2

3

4

5

6

7

# min hour day month weekday command

*/15****run-parts/etc/periodic/15min

0****run-parts/etc/periodic/hourly

02***run-parts/etc/periodic/daily

03**6run-parts/etc/periodic/weekly

051**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

Shell

1

2

3

4

5

6

7

8

9

10

11

12

/# crond --help

BusyBox v1.24.2(2016-06-2217:51:28GMT)multi-call binary.

Usage:crond-fbS-lN-dN-LLOGFILE-cDIR

-fForeground

-bBackground(default)

-SLog tosyslog(default)

-lNSet log level.Most verbose:0,default:8

-dNSet log level,log tostderr

-LFILELog toFILE

-cDIR 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.