部署
Docker
要使用 Docker 部署,请像往常一样构建您的 Dockerfile,运行 go generate ./...
(参见 设置 go generate),然后就可以开始了!
我们还建议使用 Go 模块 (在新标签页中打开),建议在使用 Go > =1.13 时使用。
示例 Dockerfile
您的 Dockerfile 可能如下所示。它使用 Go 模块和分层缓存来实现快速 Docker 构建。
如果您想进一步优化 Docker 镜像,请了解如何使用 多阶段构建。
FROM golang:1.20 as build
WORKDIR /workspace
# add go modules lockfiles
COPY go.mod go.sum ./
RUN go mod download
# prefetch the binaries, so that they will be cached and not downloaded on each change
RUN go run github.com/steebchen/prisma-client-go prefetch
COPY . ./
# generate the Prisma Client Go client
RUN go run github.com/steebchen/prisma-client-go generate
# or, if you use go generate to run the generator, use the following line instead
# RUN go generate ./...
# build the binary with all dependencies
RUN go build -o /app .
CMD ["/app"]
优化后的 Dockerfile
如果您想进一步优化 Docker 镜像,可以使用多阶段构建。这将创建一个更小的镜像,其中只包含二进制文件,而不是整个构建环境。但是,您需要执行一些额外的步骤,例如复制 SSL 证书。
FROM golang:1.21.5-buster as builder
WORKDIR /workspace
# add go modules lockfiles
COPY go.mod go.sum ./
RUN go mod download
# prefetch the binaries, so that they will be cached and not downloaded on each change
RUN go run github.com/steebchen/prisma-client-go prefetch
COPY ./ ./
# generate the Prisma Client Go client
RUN go run github.com/steebchen/prisma-client-go generate
# or, if you use go generate to run the generator, use the following line instead
# RUN go generate ./...
# build a fully standalone binary with zero dependencies
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -ldflags '-extldflags "-static"' -o app .
# use the scratch image for the smallest possible image size
FROM scratch
# copy over SSL certificates, so that we can make HTTPS requests
COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ca-certificates.crt
COPY --from=builder /workspace/app /app
ENTRYPOINT ["/app"]