在x86架构下如何打包arm64架构的镜像,docker buildx 是docker的一个插件,使用qemu作为底层支撑。
组件 | 版本 |
---|---|
docker | v20.10.15 |
rocky linux (centos 8) | v8.5 |
参考:Build multi-platform images
安装
docker run --privileged --rm tonistiigi/binfmt --install all
创建builder
docker buildx create --use --name mybuild default
使用go语言为例子,参考这位大佬博客:
cat > hello.go <<EOF
package main
import (
"fmt"
"runtime"
)
func main() {
fmt.Printf("Hello, %s!\n", runtime.GOARCH)
}
EOF
Dockerfile
cat > Dockerfile <<EOF
FROM golang:alpine AS builder
ENV GO111MODULE auto
RUN mkdir /app
ADD . /app/
WORKDIR /app
RUN go build -o hello .
FROM alpine
RUN mkdir /app
WORKDIR /app
COPY --from=builder /app/hello .
CMD ["./hello"]
EOF
开始构建镜像
# 本地打包
docker buildx build -t test --platform=linux/arm64 . --load
# 在远程主机打包,需要修改docker.service文件
docker -H tcp://192.168.10.75:2375 buildx build -t test --platform=linux/arm64 . --load
正常情况下,推送Dockerhub:
docker buildx build -t oneslide/test --platform=linux/arm64 . --push
但是推送http私库推送不了,目前无人解决,参考issue 218
但是,我找到一种解决办法:
docker buildx的原理是创建一个buildkit容器, 只需要通过传入私库--config
配置
docker buildx create --use \
--name mybuilder \
--driver docker-container \
--config buildkitd.toml --use default
debug = true
# root is where all buildkit state is stored.
root = "/var/lib/buildkit"
# insecure-entitlements allows insecure entitlements, disabled by default.
insecure-entitlements = [ "network.host", "security.insecure" ]
# 你的私库地址
# optionally mirror configuration can be done by defining it as a registry.
[registry."registry.sample.com"]
http = true
然后打包
docker buildx build -t oneslide/test --platform=linux/arm64 . --push
docker buildx 二进制可执行文件,在centos 系统里位于/usr/libexec/docker
,
$ docker run --name test --rm -it -v /var/run/docker.sock:/var/run/docker.sock \
-v /usr/bin/docker:/usr/bin/docker -v /usr/libexec/docker:/usr/libexec/docker \
-v /root:/root centos:7 /bin/bash
$ docker buildx ls
$ cd /root/gotest
$ docker buildx build -t oneslide/test --platform=linux/arm64 . --push