linuxdockerapple-m1python-3.8

Python "exec /usr/local/bin/python3: exec format error" on Docker while using Apple M1 Max


My docker images were working fine as soon as I moved to my new Mac M1 Max. Even with my M1 Max, I have installed docker and successfully created image, and pushed to AWS ECR. Now when I'm running that image, it doesn't run but throws an error

exec /usr/local/bin/python3: exec format error

My Dockerfile looks like below but no luck yet. The same error everytime. I know that it's not straight forward to built docker images on Mac M1 Max and run them but in many StackOverflow answers I found that the below addition --platform=linux/arm64 helps but not in my case yet.

FROM --platform=linux/amd64 python:3.8-slim-buster
# FROM --platform=linux/arm64 python:3.8-slim-buster (tried this one as well)
# FROM --platform=linux/arm64/v8 python:3.8-slim-buster (tried this one as well)


WORKDIR /project
COPY ./requirements.txt .

RUN apt-get -qq update 
RUN pip3 --quiet install --requirement requirements.txt \
         --force-reinstall --upgrade

COPY . .


Solution

  • I got it working by just adding as build. So the first line will look like

    FROM --platform=linux/amd64 python:3.8-slim-buster as build
    

    I'm not exactly sure what's the difference between the above line and the one I was using earlier but some as build is mentioned in the docker docs. Now it works fine