-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathDockerfile.sync
More file actions
48 lines (33 loc) · 1.04 KB
/
Copy pathDockerfile.sync
File metadata and controls
48 lines (33 loc) · 1.04 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
# 使用官方 Go 映像作為建置階段
FROM golang:1.24.1-alpine AS builder
# 安裝必要的套件
RUN apk add --no-cache git ca-certificates tzdata
# 設定工作目錄
WORKDIR /app
# 複製 go.mod 和 go.sum 檔案
COPY go.mod go.sum ./
# 下載依賴
RUN go mod download
# 複製原始碼
COPY . .
# 建置應用程式
RUN CGO_ENABLED=0 GOOS=linux go build -a -ldflags='-w -s' -o sync_stock_info ./cmd/sync_stock_info
# 使用輕量級的 Alpine 映像作為執行階段
FROM alpine:latest
# 安裝 ca-certificates、tzdata 和中文字型套件
RUN apk --no-cache add ca-certificates tzdata && rm -rf /var/cache/apk/*
# 設定時區為台北時間
ENV TZ=Asia/Taipei
# 建立非 root 使用者
RUN addgroup -g 1001 -S appgroup && \
adduser -u 1001 -S appuser -G appgroup
# 設定工作目錄
WORKDIR /root/
# 從建置階段複製執行檔
COPY --from=builder /app/sync_stock_info .
# 變更檔案擁有者
RUN chown -R appuser:appgroup /root/
# 切換到非 root 使用者
USER appuser
# 執行應用程式
CMD ["./sync_stock_info"]