cat >Dockerfile<<-EOF
# Build the manager binary
FROM golang:1.21 AS builder
ARG TARGETOS
ARG TARGETARCH
WORKDIR /workspace
# Copy the Go Modules manifests
COPY go.mod go.mod
COPY go.sum go.sum
# cache deps before building and copying source so that we don't need to re-download as much
# and so that source changes don't invalidate our downloaded layer
RUN go env -w GOPROXY=https://goproxy.cn,direct
RUN go env -w GO111MODULE=on
RUN go mod download
# Copy the go source
COPY server.go server.go
COPY api/ api/
COPY contact/ contact/
# Build
# the GOARCH has not a default value to allow the binary be built according to the host where the command
# was called. For example, if we call make docker-build in a local env which has the Apple Silicon M1 SO
# the docker BUILDPLATFORM arg will be linux/arm64 when for Apple x86 it will be linux/amd64. Therefore,
# by leaving it empty we can ensure that the container and binary shipped on it will have the same platform.
RUN CGO_ENABLED=1 GOOS=linux GOARCH=amd64 go build -o server -trimpath -ldflags '-s -w -buildid= -linkmode "external" -extldflags "-static"' server.go
# Use distroless as minimal base image to package the manager binary
# Refer to https://github.com/GoogleContainerTools/distroless for more details
FROM registry.cn-beijing.aliyuncs.com/public-lib/alpine:bin
WORKDIR /
COPY --from=builder /workspace/server .
#USER 65532:65532
ENTRYPOINT ["/server"]
EOF
dockerfile多阶段构建go程序镜像
原创
©著作权归作者所有:来自51CTO博客作者清风hao的原创作品,请联系作者获取转载授权,否则将追究法律责任
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
Dockerfile 教程:构建你的 Docker 镜像
Dockerfile 教程:构建你的 Docker 镜像
Dockerfile nginx Docker -
谈谈 Docker 镜像多阶段构建
导读:通常情况下,构建镜像通常会采用两种方式:1. 将全部组件及其依赖库的编译、测试、打包等流程封装kerfile......
docker 容器 云原生 原力计划 nginx -
Docker 的多阶段构建镜像
Docker的口号是Build,Ship,and Run Any App,Anywhere,在我们使用 Docker 的大部分
docker github golang -
Dockerfile 多阶段构建实践
在Docker Engine 17.05 中引入了多阶段构建,以此降低构建复杂度,同时使缩小镜像尺寸更为简单。这篇小作文我们来学习一下如何编写实现多阶段构建的Dockerfile ...
docker golang 3c 复杂度 github