WebRtc_QingGan/Dockerfile
Song367 345533ed6e
Some checks failed
Gitea Actions Demo / Explore-Gitea-Actions (push) Failing after 4m29s
添加yaml 部署文件
2025-07-29 18:23:17 +08:00

113 lines
2.2 KiB
Docker
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 使用官方Node.js运行时作为基础镜像
FROM node:18-alpine
# 安装系统依赖和监控工具
RUN apk add --no-cache \
bash \
procps \
curl \
&& npm install -g yarn nodemon
# 设置工作目录
WORKDIR /app
# 复制package.json和yarn.lock如果存在
COPY package.json yarn.lock* ./
# 安装项目依赖
RUN yarn install --frozen-lockfile
# 复制项目文件
COPY . .
# 创建videos目录如果不存在
RUN mkdir -p videos
# 创建CPU监控和重启脚本
RUN cat > /app/monitor.sh << 'EOF'
#!/bin/bash
# CPU阈值设置百分比
CPU_THRESHOLD=80
# 检查间隔(秒)
CHECK_INTERVAL=30
# 日志文件
LOG_FILE="/app/monitor.log"
log_message() {
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1" | tee -a "$LOG_FILE"
}
get_cpu_usage() {
# 获取Node.js进程的CPU使用率
local pid=$(pgrep -f "node.*server.js" | head -1)
if [ -n "$pid" ]; then
ps -p $pid -o %cpu --no-headers | awk '{print int($1)}'
else
echo "0"
fi
}
restart_app() {
log_message "CPU使用率过高重启应用..."
pkill -f "node.*server.js"
sleep 5
cd /app
yarn dev &
log_message "应用已重启"
}
log_message "开始CPU监控阈值: ${CPU_THRESHOLD}%"
while true; do
cpu_usage=$(get_cpu_usage)
log_message "当前CPU使用率: ${cpu_usage}%"
if [ "$cpu_usage" -gt "$CPU_THRESHOLD" ]; then
log_message "警告: CPU使用率 ${cpu_usage}% 超过阈值 ${CPU_THRESHOLD}%"
restart_app
fi
sleep $CHECK_INTERVAL
done
EOF
# 给监控脚本执行权限
RUN chmod +x /app/monitor.sh
# 创建启动脚本
RUN cat > /app/start.sh << 'EOF'
#!/bin/bash
# 启动应用
echo "启动WebRTC应用..."
cd /app
yarn dev &
# 等待应用启动
sleep 10
# 启动CPU监控
echo "启动CPU监控..."
/app/monitor.sh &
# 保持容器运行
wait
EOF
# 给启动脚本执行权限
RUN chmod +x /app/start.sh
# 暴露端口默认3000支持环境变量配置
EXPOSE 3000
# 设置环境变量
ENV NODE_ENV=production
ENV PORT=3000
# 健康检查
HEALTHCHECK --interval=30s --timeout=10s --start-period=60s --retries=3 \
CMD curl -f http://localhost:$PORT || exit 1
# 启动应用和监控
CMD ["/app/start.sh"]