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

如何在Docker子镜像中运行后台监控进程?

解决Airflow Docker镜像中后台监控进程未执行的问题

问题根源

父镜像的ENTRYPOINT为/usr/bin/dumb-init -- /entrypoint,子镜像设置的CMD会作为参数传递给/entrypoint脚本,而非直接执行。同时,直接用&后台运行脚本时,若没有前台进程维持,dumb-init可能无法正确跟踪后台进程,导致脚本未启动或被回收。

具体解决办法

1. 用bash -c组合命令,保留前台进程

在子镜像的Dockerfile中,将CMD设置为包含Airflow启动命令和监控脚本的shell命令,通过wait维持前台进程:

CMD ["bash", "-c", "/entrypoint airflow scheduler & ./scheduler_health.sh & wait"]
  • 解释:/entrypoint airflow scheduler启动Airflow调度器并后台运行,随后启动监控脚本,wait会让bash进程保持前台运行,确保dumb-init不会终止容器,同时两个进程都能持续执行。

2. 自定义Entrypoint脚本,整合原有逻辑

创建一个自定义的entrypoint脚本,调用原父镜像的/entrypoint,再启动监控脚本:

  1. 编写custom_entrypoint.sh:
#!/bin/bash
# 执行原entrypoint启动Airflow服务
/entrypoint "$@" &
# 启动监控脚本
./scheduler_health.sh &
# 等待所有后台进程,保持容器运行
wait
  1. 在子镜像Dockerfile中复制脚本并设置新的ENTRYPOINT:
COPY custom_entrypoint.sh .
RUN chmod +x custom_entrypoint.sh
# 覆盖原ENTRYPOINT,保留dumb-init的进程管理
ENTRYPOINT ["/usr/bin/dumb-init", "--", "./custom_entrypoint.sh"]
# 指定Airflow启动命令,如调度器
CMD ["airflow", "scheduler"]

3. 排查脚本自身问题

  • 确保scheduler_health.sh和health.py有执行权限:
    COPY scheduler_health.sh health.py .
    RUN chmod +x scheduler_health.sh health.py
    
  • 在scheduler_health.sh中添加日志输出,排查执行情况:
    echo "监控脚本启动时间: $(date)" >> /var/log/monitor.log
    # 调用health.py时指定绝对路径或正确的Python解释器
    /usr/local/bin/python ./health.py >> /var/log/monitor.log 2>&1
    
  • 检查脚本中是否有路径错误,比如health.py的调用路径是否正确,避免因相对路径问题导致无法执行。

内容的提问来源于stack exchange,提问作者subodh

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.27 17:10:14