代码之家  ›  专栏  ›  技术社区  ›  Bruno Peixoto

Cron job和docker

  •  -1
  • Bruno Peixoto  · 技术社区  · 1 年前

    我有一个网络爬虫。它必须定期运行。我想使用Docker来托管脚本cronjob和数据库。我能够写下面的Dockerfile和docker-compose.yaml。我在命令运行`docker compose-f docker-compose.yaml build`时得到以下错误。爬网程序文件在src/main.py文件上,文件'cron-config'上的cron作业的内容为`0 0***python3/app/src/main.py>>/app/logs/cron.log 2>&1.你能看出我哪里失败了吗?

    错误日志:

    > [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
    

    任何帮助都将不胜感激。

    2 回复  |  直到 1 年前
        1
  •  1
  •   Seth Speaks    1 年前

    看起来你的大部分dockerfile都是从复制的 this 问题

    你的错误( crontab: not found )建议没有安装crontab,在链接问题的dockerfile中,他们运行的第一个命令是安装cron。

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

    如果你把它添加到你的dockerfile的顶部,它有效吗?

        2
  •  0
  •   Dimitar Hristov    1 年前

    您应该能够在docker compose设置之外复制它:

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

    基本映像似乎缺少CLI。您可以使用文档中的apt-get进行安装 here 或者简单地使用不同的基础图像。

    推荐文章