Files
ChatRoom/Dockerfile
2026-02-03 23:54:40 +08:00

47 lines
1.1 KiB
Docker
Raw 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.
# 第一阶段:构建
FROM golang:1.24-alpine AS builder
WORKDIR /app
# 安装编译依赖(仅用于 CGO但你已禁用 CGO其实可省略
# 如果确实不需要 CGO如纯 Go 代码),可删除下一行以加速构建
RUN apk add --no-cache gcc musl-dev
# 复制模块文件并下载依赖
COPY go.mod go.sum ./
RUN go mod download
# 复制源码
COPY . .
# 编译静态二进制CGO_ENABLED=0 确保无动态链接)
RUN CGO_ENABLED=0 GOOS=linux go build -trimpath -ldflags="-s -w" -o server ./cmd/server/main.go
# 第二阶段:运行
FROM alpine:latest
# 安全加固:创建非 root 用户
RUN addgroup -g 1001 -S app && \
adduser -u 1001 -S app -G app
WORKDIR /app
# 从构建阶段复制二进制和静态资源
COPY --from=builder /app/server .
COPY --from=builder /app/web ./web
# 更改文件所有者(安全最佳实践)
RUN chown -R app:app /app
# 切换到非 root 用户
USER app
# 暴露端口
EXPOSE 2779
# 设置默认端口(可通过 docker run -e PORT=... 覆盖)
ENV PORT=2779
# 启动应用
CMD ["./server"]