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

Docker Cron容器以退出码0终止且无日志排查求助

问题:Docker Compose部署cron容器启动后以退出码0终止
  • 单独用docker build运行cron容器正常,加入Compose后启动脚本执行完容器就以退出码0终止
  • Python和MySQL容器运行正常,仅cron容器出问题
  • 查看docker logs、docker compose logs及Docker Desktop日志均无有效信息,仅收到Compose版本过时警告(不影响其他容器)
  • 已尝试在Dockerfile中添加sleep和RUN tail -f /dev/null,问题未解决

相关配置文件

docker-compose_combined.yml

version: "3"

services:
  python_cron_container:
    build:
      context: .
      network: my_network
      dockerfile: docker/Dockerfile_python
    image: ubuntu:latest
    container_name: python_cron_container
    volumes:
      - /Users/my_name/python_cron_container:/scripts
    restart: always
  datacentral_server:
    build:
      context: .
      network: my_network
      dockerfile: docker/Dockerfile-mysql
    image: mysql/mysql-server:8.0
    container_name: datacentral_server
    environment:
      MYSQL_USER: 'username_here'
      MYSQL_PASSWORD: 'pword_here'
      MYSQL_ROOT_PASSWORD: 'pword_here'
    volumes:
      - ./mysql_server/init.d/mysql-dump/schema_dump_2.sql:/docker-entrypoint-initdb.d/schema__dump_2.sql
      - ./mysql-data/:/var/lib/mysql/
      - /Users/my_name/python_cron_container:/scripts
      - /Users/my_name/python_cron_container/schema1.sh:/docker-entrypoint-initdb.d/schema1.sh
      -  /Users/my_name/python_cron_container/conf.d/my.cnf:/etc/mysql/conf.d/my.cnf
    ports:
      - 3306:3306
    restart: always
  cron_container:
     build:
       context: .
       network: my_network
       dockerfile: docker/Dockerfile-cron
     image: ubuntu:18.04
     container_name: cron_container

docker/Dockerfile-cron

FROM ubuntu:18.04

# install cron and vim
RUN apt-get update\
&& apt-get install cron -y && apt-get install vim -y

# install python and some packages
#RUN apt-get install -y --no-install-recommends \
    #cron \
    #python3-setuptools \
    #python3-pip\
    #python3.12-venv\
    #pkg-config\
    #default-libmysqlclient-dev \
    #build-essential 

# Crontab file copied to cron.d directory
COPY ./cronjob etc/cron.d/container_cronjob

# Script file copied into container.
COPY ./script.sh /script.sh

# Giving exectuable permission to script file
RUN chmod +x /script.sh

# sleep
sleep 10
# Running commands for the startup of the container
CMD ["/bin/bash","-c"."/script.sh && chmod 644 etc/cron.d/container_cronjob && cron && tail -f var/log/cron.log"]

# Command to make sure container keeps running
# CMD ["cron", "-f", "-l", "8"] && tail -f /dev/null
RUN tail -f /dev/null
sleep 10

build_and_runcron.sh

#!/bin/sh
docker container rm cron_container
docker-compose -f docker-compose_combined.yml build --force-rm
docker-compose -f docker-compose_combined.yml up  cron_container

解决方案

1. 修复Dockerfile核心错误

  • CMD语法错误:原指令中"-c"."缺少空格,数组参数需用逗号分隔,且-c后的命令需作为整体参数;同时文件路径缺少根路径/,会导致文件找不到
  • 无效指令清理:直接写sleep 10不属于Dockerfile指令,会被忽略;RUN tail -f /dev/null会卡住镜像构建,完全无用,需删除
  • 提前设置权限:将crontab文件权限设置提前到构建阶段,避免启动时重复执行

修正后的Dockerfile-cron:

FROM ubuntu:18.04

# 安装依赖并清理缓存
RUN apt-get update \
    && apt-get install cron vim -y \
    && rm -rf /var/lib/apt/lists/*

# 复制crontab文件到正确路径
COPY ./cronjob /etc/cron.d/container_cronjob

# 复制脚本并设置权限
COPY ./script.sh /script.sh
RUN chmod +x /script.sh \
    && chmod 644 /etc/cron.d/container_cronjob

# 启动cron并保持容器运行(前台模式)
CMD ["cron", "-f", "-l", "8"]

2. Compose配置优化

  • 移除image: ubuntu:18.04:该配置会覆盖你构建的自定义镜像,导致使用官方基础镜像而非带cron的镜像
  • 可选添加restart: always:保证容器意外退出后自动重启

修正后的cron_container服务:

cron_container:
  build:
    context: .
    network: my_network
    dockerfile: docker/Dockerfile-cron
  container_name: cron_container
  restart: always # 按需添加

3. 调试排查步骤

  • 启动容器后直接进入调试:docker exec -it cron_container bash,手动执行cron和查看脚本输出,确认脚本是否正常执行
  • 检查/script.sh:如果脚本执行失败导致后续命令终止,可在CMD中改为/script.sh || true && cron -f -l 8,忽略脚本错误继续启动cron

内容的提问来源于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.17 06:05:58