Loading

Buildx

前提环境

  • 系统: Windows 11 专业版 22H2
  • Docker Desktop 4.8.2 (79419)
  • wsl2 ubuntu 22.04

要想使用 buildx,首先要确保 Docker 版本不低于 19.03,同时还要通过设置环境变量 DOCKER_CLI_EXPERIMENTAL 来启用。可以通过下面的命令来为当前终端启用 buildx 插件:

 export DOCKER_CLI_EXPERIMENTAL=enabled

验证版本

chq@chq:~/buildx-test$ docker --version
Docker version 20.10.14, build a224086

如果在某些系统上设置环境变量 DOCKER_CLI_EXPERIMENTAL 不生效(比如 Arch Linux),你可以选择从源代码编译:

export DOCKER_BUILDKIT=1
docker build --platform=local -o . git://github.com/docker/buildx
mkdir -p ~/.docker/cli-plugins && mv buildx ~/.docker/cli-plugins/docker-buildx

启用 binfmt_misc

如果你使用的是 Docker 桌面版(MacOS 和 Windows),默认已经启用了 binfmt_misc,可以跳过这一步。

验证是 binfmt_misc 否开启:

chq@chq:~/buildx-test$ ls -al /proc/sys/fs/binfmt_misc/
total 0
drwxr-xr-x 2 root root 0 Jul  7 14:51 .
dr-xr-xr-x 1 root root 0 Jul  7 14:51 ..
-rw-r--r-- 1 root root 0 Jul  7 14:51 WSLInterop
-rw-r--r-- 1 root root 0 Jul 11 15:46 qemu-aarch64
-rw-r--r-- 1 root root 0 Jul 11 15:46 qemu-arm
-rw-r--r-- 1 root root 0 Jul 11 15:46 qemu-mips64
-rw-r--r-- 1 root root 0 Jul 11 15:46 qemu-mips64el
-rw-r--r-- 1 root root 0 Jul 11 15:46 qemu-ppc64le
-rw-r--r-- 1 root root 0 Jul 11 15:46 qemu-riscv64
-rw-r--r-- 1 root root 0 Jul 11 15:46 qemu-s390x
--w------- 1 root root 0 Jul  7 14:51 register
-rw-r--r-- 1 root root 0 Jul  7 14:51 status

从默认的构建器切换到多平台构建器

Docker 默认会使用不支持多 CPU 架构的构建器,我们需要手动切换。

先创建一个新的构建器:

chq@chq:~/buildx-test$ docker buildx create --use --name mybuild
mybuild

启动构建器:

chq@chq:~/buildx-test$ docker buildx inspect mybuild --bootstrap
[+] Building 41.7s (1/1) FINISHED
 => [internal] booting buildkit                                                                                                                       41.7s
 => => pulling image moby/buildkit:buildx-stable-1                                                                                                    40.8s
 => => creating container buildx_buildkit_mybuild0                                                                                                     0.8s
Name:   mybuild
Driver: docker-container

Nodes:
Name:      mybuild0
Endpoint:  unix:///var/run/docker.sock
Status:    running
Platforms: linux/amd64, linux/amd64/v2, linux/amd64/v3, linux/arm64, linux/riscv64, linux/ppc64le, linux/s390x, linux/386, linux/mips64le, linux/mips64, linux/arm/v7, linux/arm/v6

查看当前使用的构建器及构建器支持的 CPU 架构,可以看到支持很多 CPU 架构:

chq@chq:~/buildx-test$ docker buildx ls
NAME/NODE     DRIVER/ENDPOINT             STATUS                 PLATFORMS
mybuild *     docker-container
  mybuild0    unix:///var/run/docker.sock running                linux/amd64, linux/amd64/v2, linux/amd64/v3, linux/arm64, linux/riscv64, linux/ppc64le, linux/s390x, linux/386, linux/mips64le, linux/mips64, linux/arm/v7, linux/arm/v6
desktop-linux                             protocol not available
default       docker
  default     default                     running                linux/amd64, linux/arm64, linux/riscv64, linux/ppc64le, linux/s390x, linux/386, linux/arm/v7, linux/arm/v6

测试编译镜像

构建前准备源文件,并编写Dockerfile文件

hello.go文件:

package main

import (
   "fmt"
   "runtime"
)

func main() {
   fmt.Printf("Hello, %s!\n", runtime.GOARCH)
}

Dockerfile文件:

FROM golang:alpine AS builder
RUN mkdir /app
ADD . /app/
WORKDIR /app
RUN go mod init hello
RUN go build -o hello .

FROM alpine
RUN mkdir /app
WORKDIR /app
COPY --from=builder /app/hello .
CMD ["./hello"]

使用buildx进行交叉编译:

chq@chq:~/buildx-test$ sudo docker buildx build -t chq/hello-arch --platform=linux/arm64 -o type=docker .
[sudo] password for chq:
[+] Building 33.5s (14/16)
 => [internal] load build definition from Dockerfile                                                               0.0s
 => => transferring dockerfile: 245B                                                                               0.0s
 => [internal] load .dockerignore                                                                                  0.0s
 => => transferring context: 2B                                                                                    0.0s
 => [internal] load metadata for docker.io/library/alpine:latest                                                  16.2s
 => [internal] load metadata for docker.io/library/golang:alpine                                                   1.6s
 => [builder 1/6] FROM docker.io/library/golang:alpine@sha256:4918412049183afe42f1ecaf8f5c2a88917c2eab153ce5ecf4  17.3s
 => => resolve docker.io/library/golang:alpine@sha256:4918412049183afe42f1ecaf8f5c2a88917c2eab153ce5ecf4bf2d55c15  0.0s
 => => sha256:4918412049183afe42f1ecaf8f5c2a88917c2eab153ce5ecf4bf2d55c1507b74 1.65kB / 1.65kB                     0.0s
 => => sha256:4e6ba1f23bdaa946184b3a88627b69f1c55436b4fc6f42eb80a1e3cfa92e53c9 1.36kB / 1.36kB                     0.0s
 => => sha256:939b513d7ff818b7dbf6602c8c82e3616a5c322d170ec228aef1b7b2d4036f00 5.21kB / 5.21kB                     0.0s
 => => sha256:9b3977197b4f2147bdd31e1271f811319dcd5c2fc595f14e81f5351ab6275b99 2.10MB / 2.72MB                    17.2s
 => => sha256:1a89e8eeedd549875510e5e4e14010906a58878526814e6df25d14009856c6ff 281.90kB / 281.90kB                 0.6s
 => => sha256:94645a83ff95687e6f078c140cfcac8def006923ed4019ef74fa189e6d9f0b14 153B / 153B                         0.7s
 => => sha256:52f1d2cc2047096fccb9e9c98a434c2b4673cde748c1a935baaef4e1a3113c49 104.37MB / 104.37MB                10.2s
 => => sha256:c79ebd5bc6f15dff169efe2a6f424f776b67bdef8c956da543bd768ca7262f85 126B / 126B                         1.2s
 => => extracting sha256:9b3977197b4f2147bdd31e1271f811319dcd5c2fc595f14e81f5351ab6275b99                          0.2s
 => => extracting sha256:1a89e8eeedd549875510e5e4e14010906a58878526814e6df25d14009856c6ff                          0.1s
 => => extracting sha256:94645a83ff95687e6f078c140cfcac8def006923ed4019ef74fa189e6d9f0b14                          0.0s
 => => extracting sha256:52f1d2cc2047096fccb9e9c98a434c2b4673cde748c1a935baaef4e1a3113c49                          2.9s
 => => extracting sha256:c79ebd5bc6f15dff169efe2a6f424f776b67bdef8c956da543bd768ca7262f85                          0.0s
 => [stage-1 1/4] FROM docker.io/library/alpine@sha256:21a3deaa0d32a8057914f36584b5288d2e5ecc984380bc0118285c70f  17.3s
 => => resolve docker.io/library/alpine@sha256:21a3deaa0d32a8057914f36584b5288d2e5ecc984380bc0118285c70fa8c9300    0.0s
 => => sha256:21a3deaa0d32a8057914f36584b5288d2e5ecc984380bc0118285c70fa8c9300 1.64kB / 1.64kB                     0.0s
 => => sha256:c74f1b1166784193ea6c8f9440263b9be6cae07dfe35e32a5df7a31358ac2060 528B / 528B                         0.0s
 => => sha256:8e1d7573f448dc8d0ca13293b1768959a2528ff04be704f1f3d35fd3dbf6da3d 1.49kB / 1.49kB                     0.0s
 => => sha256:9b3977197b4f2147bdd31e1271f811319dcd5c2fc595f14e81f5351ab6275b99 2.72MB / 2.72MB                     0.8s
 => => extracting sha256:9b3977197b4f2147bdd31e1271f811319dcd5c2fc595f14e81f5351ab6275b99                          0.2s
 => [internal] load build context                                                                                  0.0s
 => => transferring context: 392B                                                                                  0.0s
 => [stage-1 2/4] RUN mkdir /app                                                                                   0.5s
 => [stage-1 3/4] WORKDIR /app                                                                                     0.0s
 => [builder 2/6] RUN mkdir /app                                                                                   0.6s
 => [builder 3/6] ADD . /app/                                                                                      0.0s
 => [builder 4/6] WORKDIR /app                                                                                     0.0s
 => [builder 5/6] RUN go mod init hello                                                                            0.6s
 => [builder 6/6] RUN go build -o hello .                                                                          2.6s
 => [stage-1 4/4] COPY --from=builder /app/hello .                                                                 0.0s
 => exporting to image                                                                                             0.0s
 => => exporting layers                                                                                            0.0s
 => => writing image sha256:82672418757c3af71abf40c913be06f9ebc7acb403aff4b5548df2944187e22a                       0.0s
 => => naming to docker.io/chq/hello-arch                                                                          0.0s

c查看镜像信息:

运行测试:

chq@chq:~/buildx-test$ docker run --rm chq/hello-arch:latest
WARNING: The requested image's platform (linux/arm64) does not match the detected host platform (linux/amd64) and no specific platform was requested
Hello, arm64!

参考:

https://blog.csdn.net/dxylovejm123/article/details/127506574

posted @ 2024-10-28 15:20  集君  阅读(72)  评论(0编辑  收藏  举报