Docker buildx fail when trying to install python dependency with pip

I’m trying to build my docker image for a Raspberry Pi 4 from my Ubuntu machine running on a VMWare Virtual Machine.

Every time I run the command docker buildx build -t nicocaldo/accumulator . --platform linux/arm/v7 Docker build until the RUN pip install and it fails with output error:

=> ERROR [7/8] RUN pip install --requirement requirements.txt                                                                         0.3s
------
 > [7/8] RUN pip install --requirement requirements.txt:
#15 0.262 standard_init_linux.go:228: exec user process caused: exec format error
------
error: failed to solve: executor failed running [/bin/sh -c pip install --requirement requirements.txt]: exit code: 1

I have tried to use just RUN pip install pyserial without any requirements.txt file but got the same error

My Dockerfile is below

# syntax=docker/dockerfile:1

FROM python:3.8-slim-buster

WORKDIR /app

COPY *.py ./
COPY *.txt ./

ADD ./db ./db
ADD ./etc ./etc

RUN pip install --requirement requirements.txt

ADD list_ports_linux.py usr/local/lib/python3.8/site-packages/serial/tools/

ENTRYPOINT [ "python3" ]

# default
CMD [ "./daemon.py" ]

When you set --platform linux/arm/v7 Docker downloads the ARM version of the image. It contains ARM binaries which cannot run on other architecture. Yes, you could say you wanted to build and not run. In fact when you build an image Docker will create a container and execute commands inside that container. You can run docker ps in another terminal during a build process to see temporary containers without the platform parameter with only RUN sleep 100.

To run ARM binaries on AMD64 you need an emulator which is in Docker Desktop on Windows and MacOS. On linux you could use QEMU yourself. Here is a description: How to Build and Run ARM Docker Containers on x86 Hosts · MatchboxBlog

I did not try it I am not sure if it works as it is described.

The Docker documentation mentions Qemu here: Docker Buildx | Docker Documentation

1 Like