Docker 通过 buildx 使用本地镜像

Mar*_*wtt 17 docker buildx

我正在为在不同架构上运行的 docker 容器构建镜像。由于我无法始终访问互联网,因此我通常只是在有互联网的情况下拉取映像,并且 docker 使用本地映像而不是拉取新映像。当我开始使用 buildx 构建映像后,这似乎不再起作用了。有没有办法告诉docker只使用本地镜像?当我有连接时,docker 似乎会检查是否有可用的新版本,但使用本地(或缓存)图像,正如我期望的那样,无需互联网连接。

$ docker image ls
ros                     galactic          bac817d14f26   5 weeks ago    626MB
Run Code Online (Sandbox Code Playgroud)
$ docker image inspect ros:galactic
...
"Architecture": "arm64",
 "Variant": "v8",
 "Os": "linux",
...
Run Code Online (Sandbox Code Playgroud)

构建命令示例

$ docker buildx build . --platform linux/arm64
WARN[0000] No output specified for docker-container driver. Build result will only remain in the build cache. To push result image into registry use --push or to load image into docker use --load 
[+] Building 0.3s (3/3) FINISHED                                                                                                  
 => [internal] load build definition from Dockerfile                                                                         0.0s
 => => transferring dockerfile: 72B                                                                                          0.0s
 => [internal] load .dockerignore                                                                                            0.0s
 => => transferring context: 2B                                                                                              0.0s
 => ERROR [internal] load metadata for docker.io/library/ros:galactic                                                        0.0s
------
 > [internal] load metadata for docker.io/library/ros:galactic:
------
Dockerfile:1
--------------------
   1 | >>> FROM ros:galactic
   2 |     RUN "echo hello"
   3 |     
--------------------
error: failed to solve: failed to fetch anonymous token: Get "https://auth.docker.io/token?scope=repository%3Alibrary%2Fros%3Apull&service=registry.docker.io": proxyconnect tcp: dial tcp 127.0.0.1:3333: connect: connection refused
Run Code Online (Sandbox Code Playgroud)

zso*_*olt 1

我的解决方法是在 Dockerfile FROM 部分中明确声明注册表,无论是您自己的私有注册表还是 dockerhub。

例如,要使用 dockerhububuntu:latest映像,FROM ubuntu:latest我将在 Dockerfile 中写入:

FROM docker.io/library/ubuntu:latest

要使用 myprivateregistry:5000 我会使用:

FROM myprivateregistry:5000/ubuntu:latest

而且您还必须--pull=falsedocker buildx buildDOCKER_BUILDKIT=1 docker build命令设置标志。当您再次有互联网时,您可以--pull=true再次使用