Files
dorm-power-monitor/go/Dockerfile
fallensigh fe00d277ea chore: Docker 部署配置(多阶段构建+环境模板)
- 多阶段构建 → 6.2MB 镜像

- .env.example 环境模板, 敏感配置运行时注入

Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent)

Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
2026-08-23 21:29:44 +08:00

47 lines
1.5 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.
# 宿舍电费监控 - 多阶段构建
# 阶段1: 编译 Go 静态二进制
FROM golang:1.22-alpine AS builder
WORKDIR /build
# 复制源码go:embed 需要 static/ 在同一构建上下文)
COPY go.mod ./
COPY *.go ./
COPY static/ ./static/
# CGO_ENABLED=0 纯静态编译,适用于 scratch/alpine 运行
RUN CGO_ENABLED=0 GOOS=linux go build -trimpath -ldflags="-s -w" -o electric-monitor .
# 阶段2: 最小运行镜像
FROM alpine:3.20
# 时区: 定时任务需按北京时间 00:00 触发
RUN apk add --no-cache tzdata \
&& cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
&& echo "Asia/Shanghai" > /etc/timezone \
&& apk del tzdata
# 数据目录(历史记录 + 缓存挂载点)
RUN mkdir -p /app/data
WORKDIR /app
COPY --from=builder /build/electric-monitor .
# 端口与健康检查
EXPOSE 8000
HEALTHCHECK --interval=30s --timeout=5s --retries=3 \
CMD wget -qO- http://127.0.0.1:8000/api/electric > /dev/null || exit 1
# 数据持久化卷
VOLUME ["/app/data"]
# 默认值(可由 docker-compose/.env 覆盖;账号密码与加密密钥必须由运行环境注入)
ENV HOST=0.0.0.0 \
PORT=8000 \
LZU_USERNAME= \
LZU_PASSWORD= \
LZU_SERVICE_ID=29615 \
XF_PLATFORM=QHIT_EDU \
XF_AUTH_APPID=2606161276416311297 \
XF_SCHOOL_CODE=10730 \
XF_YM_APPID=1810181825222034 \
CACHE_FILE=/app/data/electric_cache.json \
HISTORY_FILE=/app/data/electric_history.json \
XF_CACHE_TTL=518400
CMD ["./electric-monitor"]