skip to Main Content

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.

2

Answers


  1. 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?

    Login or Signup to reply.
  2. You should be able to replica it outside of your docker-compose setup:

    ➜  ~ docker run -it python:3.8-slim /bin/bash
    root@xxx:/# crontab -l
    bash: crontab: command not found
    

    It seems like the base image is missing the CLI. You can either install it using apt-get as documented here or simply use a different base image.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search