linux怎么查看本机内存大小
357
2022-10-18
Dockerfile多阶段构建
多阶段构建之前的做法: 在Docker17.05版本之前,构建Docker镜像,通常采用两种方式: 1.全部放入一个Dockerfile 一种方式是将所有的构建过程全都包含在一个Dockerfile中,包括项目及其依赖库的编译、测试、打包流程,这里会带来的一些问题: 镜像层次多,镜像体积较大,部署时间变长 源代码存在泄漏的风险 例如,编写app.go文件,输出Hello World! package mainimport "fmt"func main(){ fmt.Printf("Hello World!");} 编写Dockerfile.one文件 FROM golang:1.9-alpineRUN apk --no-cache add git ca-certificatesWORKDIR /go/src/github.com/go/helloworld/COPY app.go .RUN go get -d -v github.com/go-sql-driver/mysql \&& CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o app . \&& cp /go/src/github.com/go/helloworld/app /rootWORKDIR /root/CMD ["./app"] 构建上面的镜像 docker build -t go/helloworld:1 -f Dockerfile.one . 构建完之后查下 docker image ls 输出:REPOSITORY TAG IMAGE ID CREATED SIZEgo/helloworld 1 abdac4fc2d71 18 seconds ago 260MB 启动 docker run abdac4fc2d71 ,输出 Hello World! ,说明没问题。 2.第二种方式就是分散到多个Dockerfile 事先在一个Dockerfile将项目及其依赖库编译测试打包好后,再将其拷贝到运行环境中,这种方式需要编写两个Dockerfile和一些编译脚本才能将其两个阶段自动整合起来,这种方式虽然可以很好的规避第一种方式存在的风险,但明显部署过程复杂。 例子: app.go文件--------------------------package mainimport "fmt"func main(){ fmt.Printf("Hello World!");} Dockerfile.build 文件--------------------------FROM golang:1.9-alpineRUN apk --no-cache add gitWORKDIR /go/src/github.com/go/helloworldCOPY app.go .RUN go get -d -v github.com/go-sql-driver/mysql \&& CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o app .Dockerfile.copy 文件--------------------------FROM alpine:latestRUN apk --no-cache add ca-certificatesWORKDIR /root/COPY app .CMD ["./app"]build.sh 文件--------------------------echo Building go/helloworld:builddocker build -t go/helloworld:build . -f Dockerfile.builddocker create --name extract go/helloworld:builddocker cp extract:/go/src/github.com/go/helloworld/app ./appdocker rm -f extractecho Building go/helloworld:2docker build --no-cache -t go/helloworld:2 . -f Dockerfile.copyrm ./app54/然后执行build.sh 这个脚本加个执行权限 chmod +x build.sh./build.shPS:如果是windows环境下创建的build.sh这个文件的话,直接放到linux上会有问题,报错会是说XXX文件路径不存在,我的这个就是,最简答的处理方法就是 用vim打开build.sh这个文件,然后执行:set fileformat=unix 然后再 wq 保存退出就行了。看下镜像 docker image lsREPOSITORY TAG IMAGE ID CREATED SIZEgo/helloworld 2 411821efb026 4 minutes ago 7.97MBgo/helloworld build e43c3b88e2c2 4 minutes ago 258MBgo/helloworld 1 abdac4fc2d71 2 hours ago 260MBubuntu 18.04 775349758637 4 weeks ago 64.2MBalpine latest 965ea09ff2eb 5 weeks ago 5.55MBhello-world latest fce289e99eb9 11 months ago 1.84kBgolang 1.9-alpine b0260be938c6 15 months ago 240MB最上面的那个就是了,可以跑下试试:docker run 411821efb026 可以输出Hello World!上面就是两种之前的创建镜像的方式,接下来的笔记是关于 多阶段构建的 Docker v17.05之后开始支持多阶段构建(multistage builds) Dockerfile 文件FROM golang:1.9-alpine as builderRUN apk --no-cache add gitWORKDIR /go/src/github.com/go/helloworld/RUN go get -d -v github.com/go-sql-driver/mysqlCOPY app.go .RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o app .FROM alpine:latest as prodRUN apk --no-cache add ca-certificatesWORKDIR /root/COPY --from=0 /go/src/github.com/go/helloworld/app .CMD ["./app"]开始构建 docker build -t go/helloworld:3 .跑下试试:docker run 0186e09ebd69 可以跑出来Hello World!对比下体积看看 docker image lsREPOSITORY TAG IMAGE ID CREATED SIZEgo/helloworld 3 0186e09ebd69 36 seconds ago 7.97MBgo/helloworld 2 411821efb026 55 minutes ago 7.97MBgo/helloworld build e43c3b88e2c2 55 minutes ago 258MBgo/helloworld 1 abdac4fc2d71 3 hours ago 260MBTAG为3的这个镜像非常小。只构建某一阶段的镜像使用 as 来为某一阶段命名,例如 FROM golang:1.9-alpine as builder如当只想构建 builder 阶段的镜像时,增加 --target=builder 参数即可docker build --target builder -t username/imagename:tag .构建时从其他镜像复制文件 使用 COPY --from=0例子:COPY --from=0 /go/src/github.com/go/helloworld/app .COPY --from=nginx:latest /etc/nginx/nginx.conf /nginx.conf
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~