Docker cron job

12/6/2019

I need to schedule some cron jobs in docker/kubernetes environment, which will make some external service calls using curl commands. I was trying to use plain alpine:3.6 image but it doesn't have curl.

Any suggestion what base image will be good for this purpose? Also, it will be helpful if there are some examples.

-- Baharul
cron
docker
kubernetes

2 Answers

12/9/2019

I am able to run curl as cronjob job using alpine image as

FROM alpine:3.6

RUN apk --no-cache add curl bash
RUN apk add --no-cache tzdata

ENV TZ=Asia/Kolkata
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ /etc/timezone

RUN mkdir -p /var/log/cron \
    && touch /var/log/cron/cron.log \
    && mkdir -m 0644 -p /etc/cron.d

ADD curlurl /etc/crontabs/root
CMD bash -c "crond -L /var/log/cron/cron.log && tail -F /var/log/cron/cron.log"

#ENTRYPOINT /bin/bash

Where curlurl is having simple curl command as cron job

* * * * * /usr/bin/curl http://dummy.restapiexample.com/api/v1/employees >> /var/log/cron/cron.log
# Leave one blank line below or cron will fail

This is working fine . But now I have tried to make alpine base image with push in private repo to avoid downloading every time. But it stopped working. How can i debug why cron job is not running anymore. Below is the way I have used.

BaseImage

FROM alpine:3.6

RUN apk --no-cache add curl bash
RUN apk add --no-cache tzdata

ENTRYPOINT /bin/bash

Docker for curl

FROM my/alpine:3.6

ENV TZ=Asia/Kolkata
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ /etc/timezone

RUN mkdir -p /var/log/cron \
    && touch /var/log/cron/cron.log \
    && mkdir -m 0644 -p /etc/cron.d

ADD curlurl /etc/crontabs/root
CMD bash -c "crond -L /var/log/cron/cron.log && tail -F /var/log/cron/cron.log"

After making it like this I am not able to get any cron output . Any suggestion on how can I debug it.

-- Baharul
Source: StackOverflow

12/6/2019

Prepare your own docker image which will includes packages you need or just use something like this https://github.com/aylei/kubectl-debug

-- k0chan
Source: StackOverflow