构建派生图像时无法在缓存中找到(本地)图像

新 Mac 上的错误

在全新的 Macbook(M1 Apple 芯片)上构建 Docker 镜像时,我得到了这个:

$ docker build -f Dockerfile-local-dev .
[+] Building 1.4s (3/3) FINISHED
 => [internal] load build definition from Dockerfile-local-dev                                                                                            0.0s
 => => transferring dockerfile: 47B                                                                                                                       0.0s
 => [internal] load .dockerignore                                                                                                                         0.0s
 => => transferring context: 2B                                                                                                                           0.0s
 => ERROR [internal] load metadata for docker.io/library/nicecorp-empty-db:latest                                                                           1.3s
------
 > [internal] load metadata for docker.io/library/nicecorp-empty-db:latest:
------
failed to solve with frontend dockerfile.v0: failed to create LLB definition: pull access denied,repository does not exist or may require authorization: server message: insufficient_scope: authorization failed

这显然与拉取策略等有关,但它应该只找到一个本地映像并使用它,而不是尝试将其从全局 Docker 注册表中拉出...

在 Ubuntu 和其他 Mac 上没有错误!

奇怪的是,这适用于我所有其他机器,运行相同的 docker build (20.10.7,build f0df350)!

$ docker build -f Dockerfile-local-dev .
[+] Building 0.1s (8/8) FINISHED
 => [internal] load build definition from Dockerfile-local-dev                                                                                                                                     0.0s
 => => transferring dockerfile: 48B                                                                                                                                                                0.0s
 => [internal] load .dockerignore                                                                                                                                                                  0.0s
 => => transferring context: 2B                                                                                                                                                                    0.0s
 => [internal] load metadata for docker.io/library/nicecorp-empty-db:latest                                                                                                                          0.0s
 => [1/4] FROM docker.io/library/nicecorp-empty-db                                                                                                                                                   0.0s
 => CACHED [2/4] RUN /etc/init.d/postgresql start &&     psql --command "CREATE DATABASE nicecorpdb_test_template OWNER nicecorpadmin;" &&     psql --command "ALTER USER nicecorpadmin SUPERUSER;"      0.0s
 => CACHED [3/4] RUN echo "host all all samenet password" >> /etc/postgresql/12/main/pg_hba.conf                                                                                                   0.0s
 => CACHED [4/4] RUN echo "max_connections = 1000" >> /etc/postgresql/12/main/postgresql.conf                                                                                                      0.0s
 => exporting to image                                                                                                                                                                             0.0s
 => => exporting layers                                                                                                                                                                            0.0s
 => => writing image sha256:06b87e72ee838bef04380d9668b4dca49efede61cf0888289a58544d1019ca1c
$ docker --version
Docker version 20.10.7,build f0df350

图像存在于本地缓存中

$ docker image ls
REPOSITORY        TAG       IMAGE ID       CREATED          SIZE
nicecorp-empty-db   latest    99c3d1659d80   20 minutes ago   393MB

在 Linux 机器(实际上是 WSL2)上对相同的代码和 Dockerfile 执行完全相同的操作时,效果非常好。和我的 Intel iMac 一样,所以这可能是与 M1 芯片和 Docker 版本有关的一些错误吗?

Dockerfile-local-dev 文件:

$ cat Dockerfile-local-dev
# Most definitely not to be used in production!
FROM nicecorp-empty-db

MAINTAINER ACME <devs@ACME.com>

# Allow nicecorpadmin user to create and drop databases (used for test runs)
RUN /etc/init.d/postgresql start && \
    psql --command "CREATE DATABASE nicecorpdb_test_template OWNER nicecorpadmin;" && \
    psql --command "ALTER USER nicecorpadmin SUPERUSER;"

# Allow all users to connect to all databases (used for test runs to allow ad-hoc databases)
RUN echo "host all all samenet password" >> /etc/postgresql/12/main/pg_hba.conf
RUN echo "max_connections = 1000" >> /etc/postgresql/12/main/postgresql.conf

运行时找到图像

当我尝试运行它时,似乎发现本地图像很好:

$ docker run nimble-empty-db:latest
 * Starting PostgreSQL 12 database server
   ...done.
2021-08-01 18:31:31.765 CEST [22] LOG:  starting PostgreSQL 12.7 (Ubuntu 12.7-1.pgdg20.04+1) on aarch64-unknown-linux-gnu,compiled by gcc (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0,64-bit
2021-08-01 18:31:31.765 CEST [22] LOG:  listening on IPv4 address "0.0.0.0",port 5432
qiu397249612 回答:构建派生图像时无法在缓存中找到(本地)图像

我遇到了类似的问题,发现这个 git 问题中的建议很有用:https://github.com/docker/for-mac/issues/5419

--platform=linux/amd64 添加到我的 FROM 命令为我解决了这个问题,所以在您的情况下:

FROM --platform=linux/amd64 nicecorp-empty-db

或者,可以设置 export DOCKER_DEFAULT_PLATFORM=linux/amd64 以避免更改实际文件。事实上,对于有问题的Ubuntu镜像,它确实存在于ARM平台,因此也可以将其设置为linux/arm64

本文链接:https://www.f2er.com/1516.html

大家都在问