前言
多阶段封装docker镜像,使用scratch镜像,尽量减小镜像包的体积。
封装用于编译的go镜像
Dockerfile
FROM golang:1.20.1 AS builder
WORKDIR /apps
COPY . /apps/
ENV CGO_ENABLED=0
ENV GOOS=linux
ENV GOARCH=amd64
ENV GOPROXY=https://goproxy.cn,direct
RUN go build -o /bin/myapp /apps/main.go
#FROM alpine:latest
FROM scratch
COPY --from=builder /bin/myapp /bin/myapp
EXPOSE 8080
CMD ["/bin/myapp"]
建议加上CGO_ENABLED=0
,不然docker容器启动时,很容易出现报错: standard_init_linux.go:228: exec user process caused: no such file or directory
用gin框架写了个简单的helloworld,基于scratch打包后的镜像体积是10MB,基于alpine是17MB,基于ubuntu镜像是78MB,基于golang是1.02GB。。。
标签:bin,封装,ENV,myapp,go,镜像,docker From: https://www.cnblogs.com/XY-Heruo/p/17166525.html