'Docker build for varying architectures with same base image not working

In my system, I need to create a large number of images for different devices with different architectures. For this, I have a pipeline that creates multiple images using Docker in Docker, all based on one Python:3.8 image, but for different architectures. In the Docker file, I specify the base image in general:

FROM python:3.8

In the build command, I specify the architecture of the particular device:

docker build --platform linux/arm/v7 --force-rm -t localhost:5000/ImageNameA

docker build --platform linux/amd64 --force-rm -t localhost:5000/ImageNameB

The problem I'm facing at the moment is that the Docker host gets confused with the intermediate images when the pipeline triggers both builds at the same time. Docker doesn't seem to be able to store intermediate images for different architectures because they only differ in digest and identical tags are overwritten. For this reason, docker only seems to select the intermediate image by the name and tag and not by the sha265: that matches the architecture. This results in the following error:

failed to get destination image "sha256:4d470a81cd7a4eb2a886d8d6ca7848d66f711ea2d44375f4ab10a97c42c1adf3": image with reference sha256: 4d470a81cd7a4eb2a886d8d6ca7848d66f711ea2d44375f4ab10a97c42c1adf3 was found but does not match the specified platform: wanted linux/arm/v7, actual: linux/amd64

Also the --force-rm option in the build command does not work here, because the deletion is done after the build process is finished. I also don't want to declare the images with @sha in the dockerfile, because the identical code should be used on different devices.

What is the best way to work around or solve this problem?



Solution 1:[1]

The only way to solve this problem for me was to include the digest of the Python image in the dockerfile:

AMD64:

FROM python:3.8@sha256:7a9aaef601387a8d5ae320243a8295412e43f9a2ca6d36594d3489418b44417a

or ARM/V7:

FROM python:3.8@sha256:45fbccbc4681e8d9ef517d43f9d0eb8f17b2beac00b0f9697bbf85354ae8a266

In this case, the Dockerfile is not general-purpose, but you can create images with the same base image for different platforms simultaneously on the same host.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1