You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Docker容器中Cronjob无法运行的问题排查求助

Docker Ubuntu容器内Cronjob无法执行的问题排查与修复

问题背景

在Ubuntu镜像构建的Docker容器中配置了Cronjob,但任务始终未执行。已确认cron服务处于运行状态,但/var/log/cron.log始终为空,以下是相关配置及排查过程:


现有配置文件

Dockerfile

FROM ubuntu:latest

# Install necessary software
RUN apt-get update && \
    apt-get -y install cron

# Add crontab file
COPY crontab /etc/cron.d/crontab

# make the directory
#RUN mkdir etc/cron.d

# create a crontab file in a new location 
RUN touch etc/cron.d/crontab


# Give execution rights on the cron job
RUN chmod 644 /etc/cron.d/crontab

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

# Start the cron service
CMD cron && tail -f /var/log/cron.log`

docker-compose.yml

version: "3"

services:
  ubuntu_cron_sql_container:
    build:
      context: .
      dockerfile: docker/Dockerfile
    image: ubuntu:latest
    container_name: ubuntu_cron_sql_container
    environment:
      MYSQL_USER: 'username'
      MYSQL_PASSWORD: 'password'
      MYSQL_ROOT_PASSWORD: 'password'
    volumes:
      - ./mysql_server/init.d:/docker-entrypoint-initdb.d
    ports:
      - 3306:3306
    restart: on-failure

启动脚本

build.sh

#!/bin/sh
docker container rm ubuntu_cron_sql_container
docker-compose -f docker-compose.yml build --force-rm

run-db.sh

#!/bin/sh
#docker container rm ubuntu_cron_sql_container

docker-compose -f docker-compose.yml up ubuntu_cron_sql_container

排查情况

  • 进入容器执行ps -ef | grep cron | grep -v grep,确认cron服务正在运行;
  • 执行/sbin/service cron start提示启动成功,但/var/log/cron.log无任何内容;
  • 现有crontab内容:
MAILTO= ( my work email is here in the actual file)
*/10 * * * * echo 'ABC'  >> /var/log/cron.log 
*/10 * * * * /Users/myname/cron_docker_practice/kitten.sh
*/10 * * * * echo 'TESTING'
*/10 * * * * echo " Running cronjob in Ubuntu container"
*/10 * * * * echo "Cronjob running in ubuntu container" >> /var/log/cron.log

修复方案

1. 修正Dockerfile核心错误

  • 移除RUN touch etc/cron.d/crontab:这条命令会清空你通过COPY导入的crontab文件;
  • 修正CMD语法:原命令末尾多余反引号,改用exec格式确保信号传递;
  • 添加crontab /etc/cron.d/crontab命令,确保cron加载任务配置;

修改后的Dockerfile:

FROM ubuntu:latest

# Install necessary software
RUN apt-get update && \
    apt-get -y install cron && \
    apt-get clean && rm -rf /var/lib/apt/lists/*

# Add crontab file
COPY crontab /etc/cron.d/my-cron-job

# Give execution rights on the cron job
RUN chmod 644 /etc/cron.d/my-cron-job

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

# Load cron job configuration
RUN crontab /etc/cron.d/my-cron-job

# Start cron and tail log with proper shell execution
CMD ["sh", "-c", "cron && tail -f /var/log/cron.log"]

2. 修复crontab配置问题

  • 修正MAILTO格式:去掉括号,改为MAILTO=your-work-email@domain.com;
  • 替换宿主机路径:容器无法访问/Users/myname/...,需将脚本挂载或复制到容器内;
  • 添加用户标识与错误日志输出:指定root用户,并将stderr重定向到日志,方便排查错误;

修改后的crontab:

MAILTO=your-work-email@example.com
*/10 * * * * root echo 'ABC' >> /var/log/cron.log 2>&1
*/10 * * * * root /usr/local/bin/kitten.sh >> /var/log/cron.log 2>&1
*/10 * * * * root echo 'TESTING' >> /var/log/cron.log 2>&1
*/10 * * * * root echo "Running cronjob in Ubuntu container" >> /var/log/cron.log 2>&1

3. 调整docker-compose配置

  • 挂载宿主机脚本到容器内:添加- ./kitten.sh:/usr/local/bin/kitten.sh卷挂载;
  • 修改镜像tag:避免使用ubuntu:latest与官方镜像冲突;

修改后的docker-compose.yml片段:

services:
  ubuntu_cron_sql_container:
    build:
      context: .
      dockerfile: docker/Dockerfile
    image: ubuntu-cron-sql:latest
    container_name: ubuntu_cron_sql_container
    environment:
      MYSQL_USER: 'username'
      MYSQL_PASSWORD: 'password'
      MYSQL_ROOT_PASSWORD: 'password'
    volumes:
      - ./mysql_server/init.d:/docker-entrypoint-initdb.d
      - ./kitten.sh:/usr/local/bin/kitten.sh
    ports:
      - 3306:3306
    restart: on-failure

4. 验证步骤

  1. 重新构建镜像:执行./build.sh;
  2. 启动容器:执行./run-db.sh;
  3. 查看日志:docker exec -it ubuntu_cron_sql_container tail -f /var/log/cron.log;
  4. 检查已加载的cron任务:docker exec -it ubuntu_cron_sql_container crontab -l。

内容的提问来源于stack exchange,提问作者Muzhdah-W

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.18 14:37:16