dqrm8199 2018-09-16 19:03
浏览 143
已采纳

用于golang的Docker多阶段构建可创建大形象

I want to use the multi stage build for my gaoling project, when I build the project locally for linux/windows/Mac I got 12.6 mb of size , I’ve currently small gaoling CLI program.

Now I want to build it with to build from it lightwhigt docker image with the scratch option and build as

I use the following, but when I check the image, I see that the size it 366MB , any idea what am I missing here? It should be less then 20MB…

#build stage
FROM golang:alpine as builder
WORKDIR /go/src/tzf
ADD . /go/src/tzf
RUN CGO_ENABLED=0 go build -ldflags '-extldflags "-static"' -o ova tzf
RUN apk add --no-cache git



FROM golang:alpine 
RUN mkdir /build
ADD . /build/
WORKDIR /build
RUN CGO_ENABLED=0 go build -ldflags '-extldflags "-static"' -o ova tzf
FROM scratch
COPY --from=builder /build/main /app/
WORKDIR /app
CMD [“./ova -v"]
  • 写回答

2条回答 默认 最新

  • douyue7536 2018-09-16 19:45
    关注

    When you build your final image, be careful to copy only the exact files you want to end up in the image. It makes sense here to make your binary be the ENTRYPOINT of the image, since there's literally nothing else you can do with it.

    I might make a two-stage pipeline like so:

    # size of this stage doesn't matter; use the standard image
    FROM golang AS builder
    WORKDIR /go/src/tzf
    ADD . ./
    RUN CGO_ENABLED=0 go build -ldflags '-extldflags "-static"' -o /ova tzf
    
    FROM scratch
    # only copy the one file, may as well put it in /
    COPY --from=builder /ova /ova
    ENTRYPOINT ["/ova"]
    # if you want to launch it with default options, you can
    # CMD ["-v"]
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)