1 回答
TA贡献1725条经验 获得超7个赞
尝试以下Docker文件:
# GO Repo base repo
FROM golang:1.12.0-alpine3.9 as builder
RUN apk add git
# Add Maintainer Info
LABEL maintainer="<>"
RUN mkdir /app
ADD . /app
WORKDIR /app
COPY go.mod go.sum ./
# Download all the dependencies
RUN go mod download
COPY . .
# Build the Go app
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o main .
# GO Repo base repo
FROM alpine:latest
RUN apk --no-cache add ca-certificates curl
RUN mkdir /app
WORKDIR /app/
# Copy the Pre-built binary file from the previous stage
COPY --from=builder /app/main .
# Expose port 8000
EXPOSE 8000
# Run Executable
CMD ["./main"]
在这里,我们创建一个中间docker builder容器,将代码复制到其中,在builder容器内构建代码,然后将binary映像复制到实际的 docker。
这将有助于在最终容器中包含所有依赖项,而且最终图像的大小将非常小
- 1 回答
- 0 关注
- 133 浏览
添加回答
举报