init
This commit is contained in:
37
Dockerfile
Normal file
37
Dockerfile
Normal file
@@ -0,0 +1,37 @@
|
||||
# 第一阶段:构建
|
||||
FROM golang:1.23-alpine AS builder
|
||||
|
||||
# 设置工作目录
|
||||
WORKDIR /app
|
||||
|
||||
# 安装必要的构建工具
|
||||
RUN apk add --no-cache gcc musl-dev
|
||||
|
||||
# 复制依赖文件并下载
|
||||
COPY go.mod go.sum ./
|
||||
RUN go mod download
|
||||
|
||||
# 复制源代码
|
||||
COPY . .
|
||||
|
||||
# 编译应用
|
||||
# CGO_ENABLED=0 用于生成静态二进制文件,适合在 alpine 运行
|
||||
RUN CGO_ENABLED=0 GOOS=linux go build -o server ./cmd/server/main.go
|
||||
|
||||
# 第二阶段:运行
|
||||
FROM alpine:latest
|
||||
|
||||
WORKDIR /app
|
||||
|
||||
# 从构建阶段复制二进制文件
|
||||
COPY --from=builder /app/server .
|
||||
|
||||
# 复制静态资源文件
|
||||
COPY --from=builder /app/web ./web
|
||||
|
||||
# 暴露端口(默认 2779)
|
||||
EXPOSE 2779
|
||||
|
||||
# 运行应用(通过 PORT 环境变量可覆盖)
|
||||
ENV PORT=2779
|
||||
CMD ["./server"]
|
||||
Reference in New Issue
Block a user