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

4

11

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 . .

Salenasalene answered 22/12, 2022 at 6:16 Comment(2)
--platform=linux/amd64 might have something to do with it. M1 is a arm cpu.Psych
Used --platform=linux/arm64 as well but no luck yet.Salenasalene
S
14

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

Salenasalene answered 22/12, 2022 at 8:36 Comment(1)
this solved same issue, thanksTrudey
A
3

I had the same problem with Google Vertex AI and ARM-Macbook.

I built a ML training container locally using the docker build and the container ran the mock tests without any errors and also the actual Python code worked well, when executed from terminal.

However, the container did not work when used in the Vertex AI training job, and resulted two vague error codes: exec /usr/local/bin/python: exec format error and The replica workerpool0-0 exited with a non-zero status of 1

The problem was solved using the provided solution, by adding FROM --platform=linux/amd64 python:3.10 AS build in the beginning of the Dockerfile.

Aquavit answered 28/12, 2023 at 13:3 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Shortcut
D
1

I had the same exact issue as @Rasmus Haapaniemi, but I was using LocalModel.build_cpr_model to create a prediction container.

Amazingly, I was able to pass the entire text above, in my case "--platform=linux/amd64 python:3.11 AS build", to the base_image argument of build_cpr_model, and it gets passed directly to the FROM statement of the Dockerfile under the hood, and solves the problem.

Downpour answered 8/2, 2024 at 0:22 Comment(0)
H
0

This can happen when the image is built for the wrong architecture.

On ARM processors use:

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

On AMD/Intel processors use:

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

You can find more information in the docs: Multi-platform images

Hhd answered 10/6, 2024 at 14:29 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.