Spaces:
Running
Running
# 使用轻量级 Alpine 作为基础构建镜像,体积小、安全性高 | |
FROM alpine AS builder | |
# 安装必需的软件包: | |
# nodejs 和 npm 用于运行 Node.js 应用和安装依赖 | |
# git 用于拉取项目代码 | |
# curl 和 jq 用于脚本处理(网络请求和JSON解析) | |
# python3 及相关开发包和 pip,用于运行 Python 脚本和安装依赖 | |
RUN apk add --no-cache nodejs npm git curl jq python3 python3-dev py3-pip | |
# 创建一个普通用户 app,避免用 root 运行容器,提高安全性 | |
RUN adduser -D app | |
# 切换到 app 用户身份,后续操作都以 app 用户执行 | |
USER app | |
# 设置工作目录为 /home/app | |
WORKDIR /home/app | |
# 克隆你的项目源码(这里是 site-alert 项目) | |
RUN git clone https://github.com/vipmc838/site-alert.git site-alert | |
# 进入项目目录 | |
WORKDIR /home/app/site-alert | |
# 安装生产依赖(忽略开发依赖),并执行构建脚本下载前端资源 | |
RUN npm ci --omit dev && npm run download-dist | |
# 设置 Python 虚拟环境路径,避免影响系统环境 | |
ENV VIRTUAL_ENV=/home/app/venv | |
# 创建 Python 虚拟环境 | |
RUN python3 -m venv $VIRTUAL_ENV | |
# 把虚拟环境的 bin 目录加入 PATH,确保运行 Python 和 pip 时使用虚拟环境 | |
ENV PATH="$VIRTUAL_ENV/bin:$PATH" | |
# 安装 Python 依赖包 requests 和 webdavclient3,用于网络请求和 WebDAV 交互 | |
RUN pip install --no-cache-dir requests webdavclient3 | |
# 拷贝备份同步脚本到项目目录,并把文件属主设置为 app 用户,确保权限正确 | |
COPY --chown=app:app backup_data.sh /home/app/site-alert/ | |
# 给同步脚本添加可执行权限 | |
RUN chmod +x /home/app/site-alert/backup_data.sh | |
# 容器对外暴露端口 3001(根据你的项目端口) | |
EXPOSE 3001 | |
# 容器启动时,后台先运行备份脚本,等待30秒后启动主程序(node server/server.js) | |
CMD ["/bin/sh", "-c", "./backup_data.sh & sleep 30 && node server/server.js"] | |