Docker:找到镜像但与指定平台不匹配(尝试在 Apple M1 上构建 amd64)

Col*_*air 13 docker docker-compose apple-m1

免责声明:我是 Docker 新手,知道自己的无知。

\n

我正在尝试使用 Apple M1 Pro 在计算机上构建具有 amd64 架构的 Docker 容器。我已platform: linux/amd64在Dockerfiledocker-compose.ymlFROM --platform=linux/amd64Dockerfile 中指定(同时和单独)。

\n

如果我将它包含在该容器的 Dockerfile 中,它构建得很好,但uname -a在该 shell 中显示aarch64架构(不是x86_64)。

\n

如果我将其包含在 中docker-compose.yml,则会出现以下错误:

\n
Error response from daemon: image with reference api:latest was found but does not match the specified platform: wanted linux/amd64, actual: linux/arm64/v8\n
Run Code Online (Sandbox Code Playgroud)\n

api我正在尝试构建的容器之一在哪里docker-compose

\n

文件结构:

\n
.\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 api\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 Dockerfile-API\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 entrypoint.sh\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 ...\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 db\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 ...\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 docker-compose.yml\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 nginx\n    \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 ...\n
Run Code Online (Sandbox Code Playgroud)\n

./docker-compose.yml

\n
Error response from daemon: image with reference api:latest was found but does not match the specified platform: wanted linux/amd64, actual: linux/arm64/v8\n
Run Code Online (Sandbox Code Playgroud)\n

./api/Dockerfile-API

\n
FROM --platform=linux/amd64 python3.6 # line in question\n...\n
Run Code Online (Sandbox Code Playgroud)\n

.从平台设置的目录docker-compose.yml

\n
.\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 api\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 Dockerfile-API\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 entrypoint.sh\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 ...\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 db\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 ...\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 docker-compose.yml\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 nginx\n    \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 ...\n
Run Code Online (Sandbox Code Playgroud)\n

.从平台设置的目录./api/Dockerfile-API

\n
version: \'3.0\'\nservices:\n  db:\n    ...\n  api:\n    platform: linux/amd64 # line in question\n    build:\n      context: ./api\n      dockerfile: Dockerfile-API\n    command: ./run.sh\n    depends_on:\n      - db\n    env_file: local.env\n    networks:\n      - nginx_network\n    expose:\n      - "8000"\n    ports:\n      - "3000:3000"\n    image: api:latest\n    ...\n  nginx:\n    ...\n\nnetworks:\n  nginx_network:\n    external: true\n\nvolumes:\n  ...\n
Run Code Online (Sandbox Code Playgroud)\n

我很感激任何关于为什么会发生这种情况以及如何解决它的想法/建议。我的机器上安装了 Rosetta 2,并且 Docker 是最新的。谢谢你!

\n

Col*_*air 10

事实证明,这docker-compose down并不等同于删除图像。通过 Docker Desktop GUI删除api映像然后重新构建 ( docker-compose up) 后,我能够创建具有x86_64架构的容器。

  • 您还可以执行 `up --build` 来强制重新构建映像,请参阅 https://docs.docker.com/engine/reference/commandline/compose_up/#options — 请注意,当不使用 `docker- compose` 但只是 `docker build`,我必须在选项中添加 `--no-cache` 以强制它从 AMD64 映像启动。 (3认同)