其他分享
首页 > 其他分享> > 多CPU构建镜像

多CPU构建镜像

作者:互联网

构建基于多CPU的镜像包,试用的是docker buildx

Docker 19.03及以上的版本支持docker buildx build命令使用 BuildKit 来构建镜像。通过--platform参数可以支持构建多架构的Docker镜像。

 

新建builder实例:

由于Docker默认的builder实例不支持同时指定多个--platform,所以必须先创建一个新的builder实例。使用如下命令

docker buildx create --use --name=mybuilder-cn --driver docker-container --driver-opt image=dockerpracticesig/buildkit:master

可以使用 docker buildx ls来查看当前的实例

基于GO语言的多版本构建

main.go

package main
import "fmt"

func main(){
        fmt.Println("Hello golang!")
}

 

Dockerfile

tianxingdeMacBook-Pro:go-01 tianxing$ cat Dockerfile 
FROM --platform=$BUILDPLATFORM  golang:1.14 as builder

ARG TARGETARCH

WORKDIR /app
COPY main.go /app/main.go
RUN mkdir /app/output
RUN GOOS=linux GOARCH=$TARGETARCH go build -a -o output/main  main.go


FROM alpine:latest
WORKDIR /root
COPY --from=builder /app/output/main .
CMD /root/main
tianxingdeMacBook-Pro:go-01 tianxing$ 

 

tianxingdeMacBook-Pro:go-web tianxing$ docker buildx build -t swr.cn-north-4.myhuaweicloud.com/metrics/go-web:v1.0.1 --platform=linux/arm64,linux/amd64 .  --push
[+] Building 41.6s (23/24)                                                                                                                                                           
 => [internal] load build definition from Dockerfile                                                                                                                            0.0s
 => => transferring dockerfile: 331B                                                                                                                                            0.0s
 => [internal] load .dockerignore                                                                                                                                               0.0s
 => => transferring context: 2B                                                                                                                                                 0.0s
 => [linux/arm64 internal] load metadata for docker.io/library/alpine:latest                                                                                                    6.4s
 => [linux/amd64 internal] load metadata for docker.io/library/golang:1.14                                                                                                     41.4s
 => [linux/amd64 internal] load metadata for docker.io/library/alpine:latest                                                                                                    6.3s
 => [auth] library/golang:pull token for registry-1.docker.io                                                                                                                   0.0s
 => [auth] library/alpine:pull token for registry-1.docker.io                                                                                                                   0.0s
 => [linux/amd64 builder 1/5] FROM docker.io/library/golang:1.14@sha256:1a7173b5b9a3af3e29a5837e0b2027e1c438fd1b83bbee8f221355087ad416d6                                        0.0s
 => => resolve docker.io/library/golang:1.14@sha256:1a7173b5b9a3af3e29a5837e0b2027e1c438fd1b83bbee8f221355087ad416d6                                                            0.0s
 => [linux/arm64 stage-1 1/3] FROM docker.io/library/alpine:latest@sha256:4edbd2beb5f78b1014028f4fbb99f3237d9561100b6881aabbf5acce2c4f9454                                      0.0s
 => => resolve docker.io/library/alpine:latest@sha256:4edbd2beb5f78b1014028f4fbb99f3237d9561100b6881aabbf5acce2c4f9454                                                          0.0s
 => [linux/amd64 stage-1 1/3] FROM docker.io/library/alpine:latest@sha256:4edbd2beb5f78b1014028f4fbb99f3237d9561100b6881aabbf5acce2c4f9454                                      0.0s
 => => resolve docker.io/library/alpine:latest@sha256:4edbd2beb5f78b1014028f4fbb99f3237d9561100b6881aabbf5acce2c4f9454                                                          0.0s
 => [internal] load build context                                                                                                                                               0.0s
 => => transferring context: 432B                                                                                                                                               0.0s
 => CACHED [linux/amd64 builder 2/5] WORKDIR /app                                                                                                                               0.0s
 => [linux/amd64 builder 3/5] COPY main.go /app/main.go                                                                                                                         0.0s
 => [linux/amd64 builder 4/5] RUN mkdir /app/output                                                                                                                             0.1s
 => [linux/amd64 builder 4/5] RUN mkdir /app/output                                                                                                                             0.1s
 => [linux/amd64 builder 5/5] RUN GOOS=linux GOARCH=arm64 go build -a -o output/main  main.go                                                                                  24.5s
 => [linux/amd64 builder 5/5] RUN GOOS=linux GOARCH=amd64 go build -a -o output/main  main.go                                                                                  25.8s
 => CACHED [linux/arm64 stage-1 2/3] WORKDIR /root                                                                                                                              0.0s
 => [linux/arm64 stage-1 3/3] COPY --from=builder /app/output/main .                                                                                                            0.1s
 => CACHED [linux/amd64 stage-1 2/3] WORKDIR /root                                                                                                                              0.0s
 => [linux/amd64 stage-1 3/3] COPY --from=builder /app/output/main .                                                                                                            0.0s
 => exporting to image                                                                                                                                                          8.7s
 => => exporting layers                                                                                                                                                         0.6s
 => => exporting manifest sha256:13356a0c40ffbddf60f463f7b2e49e8838db0694d15fadb150327d0b892ec7ca                                                                               0.0s
 => => exporting config sha256:b0ebb435e5a6c84ef45cc8394495f16da523ec36e06f662e4e77f9191bf9ee21                                                                                 0.0s
 => => exporting manifest sha256:9a494feed8350b8bba4413923b4661552063279d65c90409a4f51b2d57ce651a                                                                               0.0s
 => => exporting config sha256:5fa0e67792be756550cf2dc203e8937b5f35fbdbcca853ec59a156214b28e732                                                                                 0.0s
 => => exporting manifest list sha256:75f6efa3fd2bbc892866858da7fc3dade540193fd6166d357836b7046e89e783                                                                          0.0s
 => => pushing layers                                                                                                                                                           6.8s
 => => pushing manifest for swr.cn-north-4.myhuaweicloud.com/metrics/go-web:v1.0.1@sha256:75f6efa3fd2bbc892866858da7fc3dade540193fd6166d357836b7046e89e783                      1.2s
 => [auth] metrics/go-web:,pull,push token for swr.cn-north-4.myhuaweicloud.com                                                                                                 0.0s
 => [auth] metrics/go-web:,pull,push token for swr.cn-north-4.myhuaweicloud.com                                                                                                 0.0s
tianxingdeMacBook-Pro:go-web tianxing$ 

 

ARM机器上验证

[root@ecs-arm-ky-002 ~]# docker pull swr.cn-north-4.myhuaweicloud.com/metrics/go-tx:v1.0.1
v1.0.1: Pulling from metrics/go-tx
9981e73032c8: Pull complete 
d4acba5e453a: Pull complete 
Digest: sha256:05811553ec28ec12ee1e71a94e88da6dc91898a2c699b21a48743c7196cb92f3
Status: Downloaded newer image for swr.cn-north-4.myhuaweicloud.com/metrics/go-tx:v1.0.1
swr.cn-north-4.myhuaweicloud.com/metrics/go-tx:v1.0.1
[root@ecs-arm-ky-002 ~]# docker run --rm swr.cn-north-4.myhuaweicloud.com/metrics/go-tx:v1.0.1
Hello golang!

AMD机器上验证

 

 

 

 

 

 

标签:amd64,0.0,构建,linux,go,镜像,docker,main,CPU
来源: https://www.cnblogs.com/tian880820/p/16159506.html