核心是entrypoint.sh脚本中的内容。
- Dockerfile案例
FROM python:3.9.16
WORKDIR /code
ADD requirements.txt /code/requirements.txt
RUN pip install -i https://pypi.tuna.tsinghua.edu.cn/simple/ -r /code/requirements.txt
RUN spacy download en_core_web_trf
ADD . .
RUN mkdir -p /data/weblog && touch /data/weblog/rms.debug.log && mkdir -p /data/cache/aminer
RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && echo 'Asia/Shanghai' >/etc/timezone
ENV PYTHONIOENCODING=utf-8
RUN chmod +x entrypoint.sh
CMD [ "./entrypoint.sh" ]
- entrypoint.sh 案例
#!/bin/bash
#nohup python manage_shenzhen.py rqworker --worker-ttl 600 &
#gunicorn --config gunicorn.conf recsys.wsgi:application
echo "start $TASK_NAME"
if [ "$TASK_NAME" = "api" ]; then
# 定义要执行的命令
commands=(
"gunicorn --config gunicorn.conf recsys.wsgi:application &"
)
elif [ "$TASK_NAME" = "task" ]; then
commands=(
"gunicorn --config gunicorn.conf recsys.wsgi:application &"
"python run_script.py task/cron_export_user_log.py &"
"python run_script.py task/cron_rec_train.py &"
)
fi
# 启动命令并保存PID
pids=()
cmd_names=()
for cmd in "${commands[@]}"; do
eval "$cmd"
pid=$!
pids+=($pid)
cmd_names+=("$cmd")
done
# 监控进程的状态
while true; do
for i in "${!pids[@]}"; do
pid=${pids[$i]}
cmd=${cmd_names[$i]}
if ! kill -0 $pid > /dev/null 2>&1; then
echo "Command '$cmd' with PID $pid has failed. Restarting container..."
exit 1 # 退出容器,使其触发 Docker 重启策略
fi
done
sleep 5
done
- build 镜像后运行
docker build . -t rms-django:v$1
docker stop rms-recsys
docker rm rms-recsys
docker run -ti -d --name rms-recsys -p 8000:8000 \
-e TASK_NAME=$1 --restart="always" \
-e DJANGO_SETTINGS_MODULE="recsys.settings_shenzhen" \
-v /data/cache/aminer:/data/cache/aminer \
-v /home/gkx/logs/rms-recsys:/data/weblog \
-v /home/gkx/rms/appserver/recsys/nltk_data:/root/nltk_data \
rms-django:v$2
标签:rms,gunicorn,容器,--,cmd,报错,Docker,data,recsys
From: https://www.cnblogs.com/beihangxuwei/p/18282247