38

014--docker编译go项目最小化

 5 years ago
source link: https://studygolang.com/articles/16008?amp%3Butm_medium=referral
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

1、参考网址

2、镜像打包

1、使用golang打出来的包太大了,非常占用内存300M+

FROM golang:latest
MAINTAINER Razil "[email protected]"
ENV GOPATH /apps/enzogo/
WORKDIR $GOPATH/src/enzoism/helloEnzo
ADD . $GOPATH/src/enzoism/helloEnzo
EXPOSE 6064
ENTRYPOINT  ["./helloEnzo"]

2、使用scratch进行打包操作只有30M+

FROM scratch
MAINTAINER enzo "https://github.com/enzoism"
ENV GOPATH /apps/enzogo/
WORKDIR /apps/enzogo/src/enzoism/helloEnzo
COPY . /apps/enzogo/src/enzoism/helloEnzo
ADD main /
ENTRYPOINT ["/main"]

3、最小化包的打包流程

  • 1、创建项目:bee new lirong
  • 2、进行静态编译:CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o main .
3mYVBjM.png!web
  • 3、编译之后会生成一个main可执行文件
FfQVzeY.png!web
  • 4、编写Dockerfile文件(环境变量+拷贝文件+解压文件+执行脚本)
FROM scratch
MAINTAINER enzo "https://github.com/enzoism"
ENV GOPATH /apps/enzogo/
WORKDIR /apps/enzogo/src/enzoism/helloEnzo
COPY . /apps/enzogo/src/enzoism/helloEnzo
ADD main /
ENTRYPOINT ["/main"]
  • 5、打包镜像

    docker build -t lirong:1.0.0 .

    docker run -id -p 8085:8080 --name lirong lirong:1.0.0

NruYJrn.png!web
  • 6、访问网址即可
Uj6niuF.png!web
  • 7、失败图片展示
ieIjauV.png!web

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK