Files
alex e1f2516374 實現 Dockerfile:多階段建置(golang:1.26-alpine → scratch)
- 建置階段:BUILDPLATFORM 原生執行、交叉編譯至目標平臺,BuildKit
  快取掛載(go mod download / build cache)加速重複建置
- 執行階段:scratch+單一靜態執行檔(無 CGO、模板與樣式 go:embed
  內嵌),以非 root UID 65532 執行,ENTRYPOINT 採 exec 形式直接接收訊號
- .dockerignore 排除 .git、tools/、Markdown 等非建置所需檔案
- README 增加 Docker 段落(建置、連資料庫容器、CLI 子指令用法)

實作 #1
2026-10-03 22:58:46 +08:00

34 lines
1.3 KiB
Docker
Raw Permalink 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.
# 多階段建置:Go 編譯 → scratch 執行映像
# 本專案為純 Go(pgx 驅動,無 CGO)、模板與樣式皆 go:embed 內嵌,
# 可編譯為單一靜態執行檔;執行階段不需要任何系統程式庫與 ca-certificates
# (服務僅對外提供 HTTP/OIDC,資料庫連線 sslmode=disable,無對外 TLS 呼叫)。
# ---- 建置階段:以 BUILDPLATFORM 原生執行,交叉編譯到目標平臺 ----
FROM --platform=$BUILDPLATFORM golang:1.26-alpine AS build
WORKDIR /src
# 先複製依賴清單,利用層快取加速重複建置
COPY go.mod go.sum ./
RUN --mount=type=cache,target=/go/pkg/mod \
go mod download
COPY . .
ARG TARGETOS
ARG TARGETARCH
RUN --mount=type=cache,target=/go/pkg/mod \
--mount=type=cache,target=/root/.cache/go-build \
CGO_ENABLED=0 GOOS=$TARGETOS GOARCH=$TARGETARCH \
go build -trimpath -ldflags="-s -w" -o /out/alterminal ./cmd/alterminal
# ---- 執行階段:scratch,僅含靜態執行檔 ----
FROM scratch
# 非 root 執行(scratch 無 /etc/passwd,以數字 UID 呈現)
COPY --from=build --chown=65532:65532 /out/alterminal /alterminal
USER 65532:65532
# 服務寫死監聽 :8080(cmd/alterminal/main.go:http.ListenAndServe(":8080", ...))
EXPOSE 8080
# 容器內無 shell,ENTRYPOINT 採 exec 形式(PID 1 即為服務本體,直接收訊號)
ENTRYPOINT ["/alterminal"]