Files
urbanLifeline/docker/example/Dockerfile.web
2025-12-28 18:30:17 +08:00

53 lines
1.7 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.

# ====================================
# 前端服务镜像 - School News Web
# 使用Node运行Vite预览服务器
# 注意dist目录需要在主机中先构建好
# ====================================
FROM node:20-alpine
# 设置环境变量
ENV TZ=Asia/Shanghai \
NODE_ENV=production \
NPM_CONFIG_REGISTRY=https://registry.npmmirror.com
# 1. 基础系统配置(这层很少变化)
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.ustc.edu.cn/g' /etc/apk/repositories && \
apk update && \
apk add --no-cache tzdata bash curl && \
ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && \
echo "Asia/Shanghai" > /etc/timezone && \
# 创建必要目录
mkdir -p /app/dist /app/config /app/logs
# 设置工作目录
WORKDIR /app
# 2. 安装依赖这层在package*.json不变时会使用缓存
COPY schoolNewsWeb/package*.json ./
RUN npm ci --only=production && \
npm install -g vite
# 3. 复制静态配置和启动脚本(这些文件不常变化)
COPY schoolNewsWeb/public/app-config.js /app/config/app-config.js.template
COPY schoolNewsWeb/docker/start.sh /app/start.sh
RUN chmod +x /app/start.sh
# 4. 复制构建产物(这行变化最频繁,放在最后)
# 注意确保在主机上已经执行过构建npm run build
COPY schoolNewsWeb/dist/ /app/dist/
# 5. 确保dist中有默认配置文件
RUN if [ ! -f /app/dist/app-config.js ]; then \
cp /app/config/app-config.js.template /app/dist/app-config.js; \
fi
# 暴露端口Vite preview默认4173
EXPOSE 4173
# 健康检查
HEALTHCHECK --interval=30s --timeout=10s --start-period=20s --retries=3 \
CMD curl -f http://localhost:4173/ || exit 1
# 启动应用
CMD ["/app/start.sh"]