-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDockerfile
More file actions
41 lines (30 loc) · 1.16 KB
/
Dockerfile
File metadata and controls
41 lines (30 loc) · 1.16 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
# Stage 1: 建置 Go 應用程式
FROM golang:1.25-alpine AS builder
# 安裝 CGO 所需的建置工具 (go-sqlite3 需要)
RUN apk --no-cache add gcc musl-dev
# 設定工作目錄
WORKDIR /app
# 複製 go.mod 和 go.sum 檔案並下載依賴項
# 這樣可以利用 Docker 的層快取,只有在依賴項變更時才重新下載
COPY go.mod go.sum* ./
RUN go mod download
# 複製所有原始碼
COPY . .
# 建置應用程式。因為 go-sqlite3 使用 CGO,所以我們不能使用 CGO_ENABLED=0。
RUN go build -o /scraper .
# Stage 2: 建立最終的、精簡的映像檔
FROM alpine:latest
# 安裝 ca-certificates (用於 HTTPS 請求) 和 sqlite-libs (go-sqlite3 執行時需要)
RUN apk --no-cache add ca-certificates sqlite-libs
WORKDIR /app
# 從 builder stage 複製編譯好的二進位檔案和設定檔
# 這樣可以確保最終映像檔只包含必要的檔案,且來源單一 (builder stage)
COPY --from=builder /scraper .
COPY --from=builder /app/config.yaml .
COPY --from=builder /app/urls.txt .
COPY --from=builder /app/user_agents.txt .
COPY --from=builder /app/proxies.txt .
# 暴露儀表板的埠
EXPOSE 8080
# 執行應用程式
CMD ["./scraper"]