-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDockerfile
More file actions
56 lines (41 loc) · 1.87 KB
/
Copy pathDockerfile
File metadata and controls
56 lines (41 loc) · 1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# ==========================================================
# 多階段建置:產出單一靜態執行檔,最終映像極小
# ==========================================================
# --- 階段一:建置 ---
# go.mod 的最低要求為 go 1.26,builder 則使用較新的穩定版 toolchain,
# Go 向下相容,不影響建置結果。版本由 Dependabot 的 docker group 自動更新。
FROM golang:1.27-alpine AS builder
WORKDIR /src
# 先複製 go.mod / go.sum 以善用 layer 快取 (相依未變時免重複下載)。
COPY go.mod go.sum ./
RUN go mod download
# 複製其餘原始碼。
COPY . .
# CGO_ENABLED=0 + 純 Go 的 SQLite driver = 完全靜態的單一執行檔。
# -ldflags 去除除錯資訊以縮小體積。
RUN CGO_ENABLED=0 GOOS=linux go build \
-trimpath \
-ldflags="-s -w" \
-o /out/weather-proxy ./cmd/weather-proxy
# --- 階段二:執行 ---
# alpine 提供 shell 與 wget (供 HEALTHCHECK),體積仍小。
FROM alpine:3.24
# ca-certificates 供 HTTPS 連線 CWA API;tzdata 讓系統層時區 (TZ) 生效。
RUN apk add --no-cache ca-certificates wget tzdata && \
addgroup -S app && adduser -S app -G app
# 預設時區設為台灣;程式本身已明確以 Asia/Taipei 處理業務時間,
# 此處再設 TZ 作為縱深防禦,使容器內 OS 層時間 (檔案 mtime 等) 亦對齊台灣。
ENV TZ=Asia/Taipei
WORKDIR /app
# 從建置階段複製執行檔。
COPY --from=builder /out/weather-proxy /app/weather-proxy
# 建立資料與日誌目錄並設定擁有者 (供 volume 掛載與非 root 寫入)。
RUN mkdir -p /app/data /app/logs && chown -R app:app /app
USER app
EXPOSE 8000
# 健康檢查:輪詢 /health 端點。
HEALTHCHECK --interval=30s --timeout=5s --start-period=10s --retries=3 \
CMD wget -qO- http://127.0.0.1:8000/health || exit 1
# 預設啟動 API 服務 (含內建排程器)。
ENTRYPOINT ["/app/weather-proxy"]
CMD ["serve"]