Docker容器Cron执行Make命令在Fedora Server异常求助
Docker容器中Cron调用Make命令在Fedora Server环境下失效的问题
我在Docker容器内配置了调用Make命令执行Python脚本的Cron任务,在Windows Docker Desktop和Mac环境中,类似* * * * * cd /code && make help的任务能正常运行,返回Makefile中定义的help输出;但在物理机Fedora Server的Linux笔记本上运行相同容器时,Make命令完全失效。
容器内的Cron任务配置在/etc/crontabs/root,执行时返回错误:
crond: USER root pid 132 cmd cd /code && make help '. Stop. No rule to make target 'help`
基础Cron任务(如* * * * * echo 'Hello world')在所有环境下均正常输出,且手动进入容器的/code目录执行make help也能正常运行。
环境与项目信息
- 项目从Windows笔记本通过FileZilla导出到Fedora Server的
notroot@localhost:~/compose/xcron目录,在此路径下执行docker compose up启动容器。 - 本地项目结构:
/xcron - .env - requirements.txt - Makefile - /api - /spotify - ... - /leetcode - ... - ...
docker-compose.yml内容:
services: xcron-app: container_name: xcronapp restart: always build: . networks: - xcron_db_network - xcron_web_network volumes: - xcronvol:/code db: container_name: xcronpostgres image: postgres restart: always ports: - "5432:5432" environment: POSTGRES_USER: ${DB_USERNAME} POSTGRES_PASSWORD: ${DB_PASSWORD} POSTGRES_DB: ${DB_NAME} volumes: - postgresvolume:/var/lib/postgresql/data healthcheck: test: ["CMD-SHELL", "pg_isready -U ${DB_USERNAME} -d ${DB_NAME}"] interval: 10s retries: 5 start_period: 30s timeout: 10s networks: - xcron_db_network networks: xcron_db_network: driver: bridge xcron_web_network: driver: bridge volumes: xcronvol: postgresvolume:
- Dockerfile内容:
FROM alpine:latest COPY config/cronjobs /etc/crontabs/root COPY requirements.txt /etc/requirements/requirements.txt ENV PYTHONUNBUFFERED=1 RUN apk add --update --no-cache python3 py3-pip make bash util-linux vim && ln -sf python3 /usr/bin/python COPY .env /code/.env COPY api /code/api COPY auth /code/auth COPY migrations/down /code/migrations/down COPY migrations/up /code/migrations/up COPY migrations/down.py /code/migrations/down.py COPY migrations/up.py /code/migrations/up.py COPY tweets /code/tweets COPY Makefile /code/Makefile RUN python3 -m venv /opt/venv ENV PATH="/opt/venv/bin:$PATH" RUN pip install -r /etc/requirements/requirements.txt RUN chmod 777 /etc/crontabs/root WORKDIR /code CMD ["crond", "-f", "-d", "8"]
- 容器内
/code目录结构(已排除Dockerfile忽略的文件):
/code - .env - Makefile - /api - /spotify - ... - /leetcode - ... - ...
已尝试的解决方案
- 为容器内的
/code/Makefile、/code/.env、/etc/crontabs/root文件添加chmod 777权限 - 修改Cron任务为绝对路径形式,例如
/usr/bin/make -C /code help,尝试多种-C、-f参数组合 - 尝试
cd /code && make -f /code/Makefile help、cd .. && make -f /code/Makefile -C /code help等命令形式 - 显式通过Bash执行任务:
/bin/bash -c 'cd /code && make help'
所有尝试后返回的错误信息完全一致,均提示No rule to make target 'help'。
内容的提问来源于stack exchange,提问作者lithium_lamp
相关产品推荐
相关产品推荐

