dockercron

Cron job and docker


I have a web crawler. It must run periodically. I want to use Docker to host both script cronjob and database. I am able to write the Dockerfile and docker-compose.yaml below. I get the error below on command run `docker-compose -f docker-compose.yaml build`. The crawler file is on file src/main.py and the content to cron job on file 'cron-config' is `0 0 * * * python3 /app/src/main.py >> /app/logs/cron.log 2>&1`. Can you see where am I failing?

Error log:

> [crawler 6/7] RUN crontab /etc/cron.d/cron-config:

0.243 /bin/sh: 1: crontab: not found

docker-compose.yaml:

version: "3.8"

services:
  crawler:
    build: 
      context: .
      dockerfile: Dockerfile
    networks:
      - CNPJ_net
    restart: unless-stopped

  crawler-db:
    image: postgres:14
    restart: always
    env_file: .env
    networks:
      - CNPJ_net
    expose:
      - "$POSTGRES_PORT"
    ports:
      - "$POSTGRES_PORT:$POSTGRES_PORT"
    volumes:
      - postgres_data:/var/lib/postgresql/data

networks:
  CNPJ_net:

volumes:
  postgres_data:

Dockerfile:

FROM python:3.8-slim

WORKDIR /app

# Copy your application code
COPY . .

# Copy the crontab file to the cron.d directory
COPY cron-config /etc/cron.d/cron-config

# Give execution rights on the cron job
RUN chmod 0644 /etc/cron.d/cron-config

# Apply cron job
RUN crontab /etc/cron.d/cron-config

# Create the log file to be able to run tail
RUN touch /var/log/cron.log

# Run the command on container startup
CMD cron && tail -f /app/logs/cron.log

Any help is appreciated.


Solution

  • Looks like most of your dockerfile was copied from this question.

    Your error (crontab: not found) suggests crontab is not installed and in that dockerfile in the linked question, the first command they run is to install cron.

    RUN apt-get update && apt-get -y install cron
    

    If you add that at the top of your dockerfile, does it work?