记一次docker buildx build 推送到本地私有仓库出现 connection refused 的问题

想在本地编译多个架构的基础镜像,这样后续有其他业务使用的时候,不必从头开始编译。
使用传统的 docker build -t ImageName:tag 方式,只能编译和主机相同架构的镜像。
docker buildx build 不支持将编译好的镜像放置在本地docker中,只能以文件的形式放在本地。因此需要在本地搭建一个私有仓库,并将编译好的多架构的基础镜像推送到私有仓库进行存储。
搭建私有仓库比较简单,使用一条命令即可搭建完毕:

docker run -d -p 5000:5000 -v /data/docker_registry:/var/lib/registry --restart=always --name registry registry

私有仓库搭建好了,先写一个简单的镜像例子来测试下:

[root@yzc ~]# mkdir alpine && cd alpine
[root@yzc alpine]# cat Dockerfile
# syntax=docker/dockerfile:1
FROM alpine:3.16
RUN apk add curl

然而在buildx推送到本地确报了这个明明奇妙的问题:

[root@yzc alpine]# docker buildx build --platform linux/amd64,linux/arm64 -t localhost:5000/myalpine:latest -o type=registry --allow network.host .
 => [internal] booting buildkit
 => => pulling image moby/buildkit:buildx-stable-1
 ... 省略一堆无关紧要的日志...
 => [linux/arm64 2/2] RUN apk add curl
 => [linux/amd64 2/2] RUN apk add curl
 => ERROR exporting to image
 => => exporting layers
 => => pushing layers
------
 > exporting to image:
------
ERROR: failed to solve: failed to push localhost:5000/myalpine:latest: failed to do request: Head "http://localhost:5000/v2/myalpine/blobs/sha256:56c7f5339f91bf07cf643df98dbc5386d3026f44072ca00913e1512a15649fb5": dial tcp 127.0.0.1:5000: connect: connection refused

看起来是docker buildx创建的实例的权限问题。

看了下文档,有人说要按照这个搞个配置文件https://github.com/moby/buildkit/blob/master/docs/buildkitd.toml.md, 于是按照这个搞了一个,没想到莫名其妙的问题太多了,于是删除了大量配置,只留了几条:

# cat ~/.config/buildkit/buildkitd.toml
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" ]

[log]
  # log formatter: json or text
  format = "text"

[dns]
  nameservers=["1.1.1.1","8.8.8.8"]
  options=["edns0"]
  #searchDomains=["example.com"]

# optionally mirror configuration can be done by defining it as a registry.
[registry."localhost:5000"]
  http = true
  insecure = true

删掉现有docker buildx的实例,重新创建:

docker buildx rm default
docker buildx create --name muilt_buildx --platform 'linux/arm64,linux/amd64,linux/amd64/v2,linux/arm/v7,linux/arm/v6,linux/amd64/v3,linux/386' --config ~/.config/buildkit/buildkitd.toml --use

还是不行,一样的错误

看了下docker buildx create的说明,百度了下相关选项说明,然后删了现有实例重新创建就好了。

Options:
      --append                   Append a node to builder instead of changing it
      --bootstrap                Boot builder after creation
      --buildkitd-flags string   Flags for buildkitd daemon
      --config string            BuildKit config file
      --driver string            Driver to use (available: "docker-container", "kubernetes", "remote")
      --driver-opt stringArray   Options for the driver
      --leave                    Remove a node from builder instead of changing it
      --name string              Builder instance name
      --node string              Create/modify node with given name
      --platform stringArray     Fixed platforms for current node
      --use                      Set the current builder instance
docker buildx rm muilt_buildx
docker buildx create --name muilt_buildx --platform 'linux/arm64,linux/amd64,linux/amd64/v2,linux/arm/v7,linux/arm/v6,linux/amd64/v3,linux/386' --driver-opt=network=host --use

再次执行上面的哪个buildx build命令即可,查看本地仓库列表:

curl 127.0.0.1:5000/v2/_catalog
{"repositories":["myalpine"]}

# 也能正常pull
# docker pull localhost:5000/myalpine:latest
latest: Pulling from myalpine
070eb51debd9: Pull complete
5761c5c4cc74: Pull complete
Digest: sha256:3b134a4faf8cb7e57efc549804e438270349ed7bfe4525edd42288d47d23e069
Status: Downloaded newer image for localhost:5000/myalpine:latest
localhost:5000/myalpine:latest

OK

posted @ 2023-12-08 23:28  川川籽  阅读(862)  评论(0编辑  收藏  举报