Repository navigation
Error building docker image for different platform/arch on M1 mac #2990
Description
Problem occured when building linux/amd64 image (by setting platform parameter) on M1 mac for different arch. I isolated a case for this bug and found workaround.
The problem is when building an image which has:
scratchas a base image- platform different than current platform, e.g. building 'linux/amd64' image on M1 mac, or building 'linux/arm64' image on amd64 machine (I tested both cases)
- ARG as the first operation
The problem does not occur when the base image is changed to anything else (e.g. ubuntu), or platform switch is not set, or there is different first op than ARG.
Build fails from code
Dockerfile:
FROM scratch
ARG dummy_arg
ADD file2.txt /data/file2.txtRun with code:
client = docker.client.from_env()
_, _ = client.images.build(path="context", dockerfile="Dockerfile", tag="test-image", platform="linux/amd64", nocache=True)Results in:
DEBUG:docker.api.build:Looking for auth config
DEBUG:docker.api.build:Sending auth config ()
DEBUG:urllib3.connectionpool:http://localhost:None "POST /v1.41/build?t=test-image-3494&q=False&nocache=True&rm=False&forcerm=False&pull=False&dockerfile=Dockerfile&platform=linux%2Famd64 HTTP/1.1" 200 None
Traceback (most recent call last):
File "/Users/lsierant/mdb/sonar-fix/test.py", line 11, in <module>
_, _ = client.images.build(path="context", dockerfile="Dockerfile", tag=image_name, platform="linux/amd64", nocache=True)
File "/Users/lsierant/mdb/sonar-fix/venv/lib/python3.9/site-packages/docker/models/images.py", line 285, in build
raise BuildError(chunk['error'], result_stream)
docker.errors.BuildError: failed to get destination image "sha256:5b89a558a786c4b82412c56c53bfdf715817ab99cd7e63bd08b496a696060c36": image with reference sha256:5b89a558a786c4b82412c56c53bfdf715817ab99cd7e63bd08b496a696060c36 was found but does not match the specified platform: wanted linux/amd64, actual: linux/arm64```
Build succeeds from cli
The same dockerfile built from cli is working correctly.
$ docker build . --platform "linux/amd64" --no-cache -t test-image
[+] Building 0.1s (5/5) FINISHED
=> => transferring dockerfile: 36B
=> [internal] load .dockerignore
=> => transferring context: 2B
=> [internal] load build context => => transferring context: 30B
=> [1/1] ADD file2.txt /data/file2.txt
=> exporting to image => => exporting layers
=> => writing image sha256:fb7f611c51c95f880cbeabf3a77067dafa8cd9cb961a6564bcba035fcc0ecef9
=> => naming to docker.io/library/test-image
Workaround
It seems that this error occurs only when having scratch image as a base and ARG as a second command. After swapping ARG and ADD lines the image is building. Also changing the base image to anything different than scratch is working.
FROM scratch
ADD file2.txt /data/file2.txt
ARG dummy_argResults in:
DEBUG:urllib3.connectionpool:http://localhost:None "POST /v1.41/build?t=test-image-5877&q=False&nocache=True&rm=False&forcerm=False&pull=False&dockerfile=Dockerfile.ok&platform=linux%2Famd64 HTTP/1.1" 200 None
DEBUG:urllib3.connectionpool:http://localhost:None "GET /v1.41/images/4491aba34a0f/json HTTP/1.1" 200 None
Steps to recreate
I created sample project when trying to pin down the issue: https://github.com/lsierant/playground-docker-py
git clone https://github.com/lsierant/playground-docker-py.git./run-test.sh(setups venv and runs test.py)
Environment
I tested it on M1 mac (building linux/arm64) and on linux machine building arm64 image.
pip freeze | grep docker && python --version && docker version
Python 3.9.13
Client:
Cloud integration: v1.0.24
Version: 20.10.14
API version: 1.41
Go version: go1.16.15
Git commit: a224086
Built: Thu Mar 24 01:49:20 2022
OS/Arch: darwin/arm64
Context: default
Experimental: true
Server: Docker Desktop 4.8.2 (79419)
Engine:
Version: 20.10.14
API version: 1.41 (minimum version 1.12)
Go version: go1.16.15
Git commit: 87a90dc
Built: Thu Mar 24 01:45:44 2022
OS/Arch: linux/arm64
Experimental: false
containerd:
Version: 1.5.11
GitCommit: 3df54a852345ae127d1fa3092b95168e4a88e2f8
runc:
Version: 1.0.3
GitCommit: v1.0.3-0-gf46b6ba
docker-init:
Version: 0.19.0
GitCommit: de40ad0
Activity
rbharathkumar04 commentedon Nov 1, 2022
I was facing this issue too. I was building a
linux/amd64image on M1 mac.The docker build command in cli worked fine, but using docker-py to build it doesn't work. I am providing
--platform=linux/amd64in both cli and docker-pyI suspected it to be a cache issue, so I tried setting nocache=True. But that didn't work.
Finally, what worked was removing the base image and letting the build process download the base image again
docker image rm <base_image:tag>chiragjn commentedon Mar 23, 2023
I am facing the same issue without any mention of
ARGin DockerfileSteps to reproduce:
docker pull --platform=linux/arm64/v8 python:3.9and
Like others said the only fix that works is to delete the base image. Docker CLI works correctly regardless.
sakcer commentedon Jul 22, 2023
You can achieve the purpose of cleaning the base image and cache by adding --pull and --no-cache flags
And this will ultimately solve the issue
d33bs commentedon Nov 6, 2025
I'm also seeing this issue. I created the following gist to help describe what happens: https://gist.github.com/d33bs/b82130dea415091c2ce3171394309ca2 .