如何在 docker 容器中的子 docker 镜像中运行后台进程

问题描述 投票:0回答:1

我正在尝试在我的气流泊坞窗图像中运行后台进程,它将继续运行以监视某些进程并发送警报。

我有一个父图像,我在我的图像中使用它,比如子图像。 这是父映像 docker 文件示例代码片段。

父 Dockerfile:

FROM ${PYTHON_AIRFLOW_IMAGE} as my_img

#all other steps like copy/creating directory etc.

EXPOSE 8080 8443

ENTRYPOINT ["/usr/bin/dumb-init", "--", "/entrypoint"]
CMD []

父图像标签

docker build -t parent_img .

我的子图像 docker 文件,我在其中使用父图像。

子镜像 Dockerfile :

FROM parent_img:latest

USER root

COPY scripts/child_airflow.sh /child_airflow.sh

RUN bash /child_airflow.sh && rm /child_airflow.sh
    
COPY scripts/scheduler_health.sh scheduler_health.sh

COPY utils/health.py health.py

CMD ./scheduler_health.sh &

scheduler_health.sh 将调用 health.py 文件。

scheduler_health.sh

#!/bin/bash

#This is a simple scheduler_health file to call health.py file

if [-e /scripts/health.py] then

echo "Scheduler Health, health.py file exist, hence calling"
 
python3 /scripts/health.py

else

echo "Scheduler health.py file doesn't exist"

fi

health.py 文件

import time
while True:
time.sleep(60)
sourceFile = open('/tmp/running.txt','a+')
print ('running, file=sourceFile)

child_airflow.sh 文件

source /bin/activate

pip install apache-airflow-providers-amazon apache-airflow-providers-celery -i

https://pypi.org/simple --no-cache-dir

bin/pip check

构建儿童形象:

docker build -t child .

运行命令:

docker run -p 8080:8080 -it child

运行子映像后,我的 health.py 文件从未被调用,看起来像 Scheduler_health.sh 没有执行。即使我也尝试在 child_airflow.sh 中调用 Scheduler_health.sh 文件,但运气不好,没有任何错误或没有 python 文件正在执行,理想情况下它应该继续在后台运行,60 秒后它应该将消息写入 /tmp/running.txt 文件。

不确定我是否遗漏了任何内容,根据文档https://docs.docker.com/config/containers/multi-service_container/它应该运行,不确定我的父入口点是否覆盖子进程或我我做错了什么。

docker shell airflow
1个回答
0
投票

我需要让 health.py 在后台运行

正如David评论,您需要确保

CMD
ENTRYPOINT
有效管理Airflow调度程序和
health.py
脚本,而不会过早退出。

这意味着

entrypoint.sh
脚本必须启动 Airflow 调度程序(或由父映像的
ENTRYPOINT
启动的主进程)和
health.py
脚本作为后台进程,然后无限期等待以保持容器运行。

#!/bin/bash
# Start the health monitoring script in the background
./scheduler_health.sh &

# Start the main process in the background as well, if necessary
# Example: /start-airflow.sh &

# The original parent image's ENTRYPOINT is expected to start the airflow scheduler
# and potentially block. If it is just an initialization script and does not block,
# then you might need to start the scheduler manually here, similar to the commented-out line above.
exec /usr/bin/dumb-init -- /entrypoint "$@"

scheduler_health.sh
脚本应按预期直接运行
health.py

运行容器时,建议使用

docker run -d ...
(“分离模式”)以确保容器在后台运行。

由于

health.py
作为后台进程运行,请确保您有足够的日志记录来监控其输出和运行状况:将其输出重定向到 Docker 的日志系统(stdout/stderr)或可访问或跟踪的文件。

© www.soinside.com 2019 - 2024. All rights reserved.