使用 buildx 构建多架构镜像另外一个例子

1. 创建一个新的 builder

  1. docker login
  2. docker buildx create --name mybuilder

2. 准备好Dockerfile

其中 TARGETARCH 这个参数会随着buildx的不同platform自动变化。然后会根据不同的platform下载不同二进制文件terraform。

类似 https://docs.docker.com/engine/reference/builder/#automatic-platform-args-in-the-global-scope 还有其他的一些自动变量可以参考链接。

Note

关于 ARGENV 的区别和用法,请参考 https://dockertips.readthedocs.io/en/latest/dockerfile-guide/env_vs_arg.html

  1. FROM alpine:3.16
  2. ARG TARGETARCH=amd64 TERRAFORM_VERSION="1.2.9"
  3. RUN apk update && apk add --no-cache curl
  4. RUN curl \
  5. --location \
  6. --output /tmp/terraform.zip \
  7. https://releases.hashicorp.com/terraform/${TERRAFORM_VERSION}/terraform_${TERRAFORM_VERSION}_linux_${TARGETARCH}.zip \
  8. && unzip /tmp/terraform.zip -d /usr/local/bin \
  9. && rm -rf /tmp/terraform.zip
  10. CMD []

3. 构建镜像

  1. $ docker buildx build --push --platform linux/arm64/v8,linux/amd64 -t xiaopeng163/terraform:1.2.9 .
  2. [+] Building 27.9s (13/13) FINISHED
  3. => [internal] load build definition from Dockerfile 0.0s
  4. => => transferring dockerfile: 460B 0.0s
  5. => [internal] load .dockerignore 0.0s
  6. => => transferring context: 2B 0.0s
  7. => [linux/arm64 internal] load metadata for docker.io/library/alpine:3.16 1.2s
  8. => [linux/amd64 internal] load metadata for docker.io/library/alpine:3.16 1.1s
  9. => [auth] library/alpine:pull token for registry-1.docker.io 0.0s
  10. => [linux/arm64 1/3] FROM docker.io/library/alpine:3.16@sha256:bc41182d7ef5ffc53a40b044e725193bc10142a1243f395ee852a8d9730fc2ad 0.0s
  11. => => resolve docker.io/library/alpine:3.16@sha256:bc41182d7ef5ffc53a40b044e725193bc10142a1243f395ee852a8d9730fc2ad 0.0s
  12. => [linux/amd64 1/3] FROM docker.io/library/alpine:3.16@sha256:bc41182d7ef5ffc53a40b044e725193bc10142a1243f395ee852a8d9730fc2ad 0.0s
  13. => => resolve docker.io/library/alpine:3.16@sha256:bc41182d7ef5ffc53a40b044e725193bc10142a1243f395ee852a8d9730fc2ad 0.0s
  14. => CACHED [linux/arm64 2/3] RUN apk update && apk add --no-cache curl 0.0s
  15. => CACHED [linux/arm64 3/3] RUN curl --location --output /tmp/terraform.zip https://releases.hashicorp.com/terraform/1.2.9/terraform_1.2.9_linux_arm64.zip && unzip /tmp/terraform.zip -d /usr/local/bin 0.0s
  16. => CACHED [linux/amd64 2/3] RUN apk update && apk add --no-cache curl 0.0s
  17. => CACHED [linux/amd64 3/3] RUN curl --location --output /tmp/terraform.zip https://releases.hashicorp.com/terraform/1.2.9/terraform_1.2.9_linux_amd64.zip && unzip /tmp/terraform.zip -d /usr/local/bin 0.0s
  18. => exporting to image 26.6s
  19. => => exporting layers 0.0s
  20. => => exporting manifest sha256:14704a82f6f432f13057d06401c8bf704b85458e8531d2dda616774bbec27cce 0.0s
  21. => => exporting config sha256:0592e737952d4a1bba5572696eee64f8aaa372127363f413c06641dfa099dac8 0.0s
  22. => => exporting manifest sha256:469a68b5e84746d1415c931378d6078a54f5aabdedad599da281f4743bc1d504 0.0s
  23. => => exporting config sha256:0b1fcf8214c11d7f696bce23d9800fed508e0526da7db0c640c079eb171a2b3b 0.0s
  24. => => exporting manifest list sha256:f137daace77c8b787d8a5cbf6b201605b62ac4b7455a56c38d5146a3fe7d55d0 0.0s
  25. => => pushing layers 25.4s
  26. => => pushing manifest for docker.io/xiaopeng163/terraform:1.2.9@sha256:f137daace77c8b787d8a5cbf6b201605b62ac4b7455a56c38d5146a3fe7d55d0 1.2s
  27. => [auth] xiaopeng163/terraform:pull,push token for registry-1.docker.io