docker-cron icon indicating copy to clipboard operation
docker-cron copied to clipboard

Crontab via docker-compose issue, alpine image

Open salvq opened this issue 2 years ago • 0 comments

Issue is when I want to link crontab via Docker-compose.yaml, crontab will not execute jobs. Where could be the issue ? I want to keep crontab outside of image and have better flexibility and avoid regenerating image everytime I change crontab.

Solution 1 is something I look for for flexibility, Solution 2 works fine but not flexible.

Solution 1: does not work but flexible - Dockerfile

FROM python:3.11.2-alpine3.17

ADD . /scrapper
WORKDIR /scrapper

RUN which crond && \
    rm -rf /etc/periodic && \
    apk add --no-cache tzdata && \
    pip install -r requirements.txt 

RUN chmod +x /scrapper/entrypoint.sh

ENTRYPOINT ["/scrapper/entrypoint.sh"]

# source: `docker run --rm -it alpine  crond -h`
# -f | Foreground
# -l N | Set log level. Most verbose 0, default 8
CMD ["crond", "-f", "-l", "2"]

- Docker-compose.yaml

version: '3.7'

services:
  scrapper:
    image: salvq/scrapper:1.0.0
    container_name: scrapper
    restart: unless-stopped
    init: true
    environment:
      - TZ=Europe/Prague
    volumes:
      - /share/Container/trade-scrapper:/trade-scrapper
      - /share/Container/trade-scrapper/config/crontab:/var/spool/cron/crontabs/root

- docker-compose executed but no job being executed

Creating trade-scrapper ... done
Attaching to trade-scrapper
trade-scrapper | crond -f -l 2

- Crontab file is present

/scrapper # cat /var/spool/cron/crontabs/root
# do daily/weekly/monthly maintenance
# 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
*       *       *       *       *       python3 /scrapper/mailscrapper.py


Solution 2: works but not flexible When I do same but COPY crontab directly in image, docker-compose works just fine

- Dockerfile

FROM python:3.11.2-alpine3.17

ADD . /scrapper
WORKDIR /scrapper

COPY crontab /var/spool/cron/crontabs/root

RUN which crond && \
    rm -rf /etc/periodic && \
    apk add --no-cache tzdata && \
    pip install -r requirements.txt 

RUN chmod +x /scrapper/entrypoint.sh

ENTRYPOINT ["/scrapper/entrypoint.sh"]

# source: `docker run --rm -it alpine  crond -h`
# -f | Foreground
# -l N | Set log level. Most verbose 0, default 8
CMD ["crond", "-f", "-l", "2"]

- Docker-compose.yaml

version: '3.7'

services:
  scrapper:
    image: salvq/scrapper:1.0.0
    container_name: scrapper
    restart: unless-stopped
    init: true
    environment:
      - TZ=Europe/Prague
    volumes:
      - /share/Container/trade-scrapper:/trade-scrapper

- Result via dockerfile is OK

Creating trade-scrapper ... done
Attaching to trade-scrapper
trade-scrapper | crond -f -l 2
trade-scrapper | Program started
trade-scrapper | Program finished
trade-scrapper | OK

salvq avatar Apr 01 '23 07:04 salvq