I have an AWS Batch job that returns the following error when it attempts to run a Docker image in ECR:
exec /usr/local/bin/python3: exec format error
I built the image on macOS with an M1 chip. Because of this I built the image with the following command using the --platform linux/amd64
parameter. I have also tried --platform linux/x86_64
:
docker buildx build --no-cache --platform linux/amd64 -t repository-name:latest .
I then ran docker inspect --format='{{.Architecture}}' repository-name:latest
and it returned arm64
. The CPU architecture for the container is X86_64. Is this where my issue lies, and if so, what is incorrect with my build step?
This is my Docker file:
FROM python:3.9
WORKDIR /usr/src/app/
COPY . /usr/src/app/
RUN pip install --no-cache-dir -r requirements.txt
EXPOSE 80
ENV NAME World
CMD ["python3", "/usr/src/app/hello_world.py"]
This is my hello_world.py script:
import pandas as pd
print('hello world!')
Additional troubleshooting:
-
Ran
docker run -it <image> bash
to verify the/usr/src/app
path is correct. -
Verified the path of the Python3 binary within docker,
which python3
. -
Ran the image,
python3 /usr/src/app/hello_world.py
and the output was correct. -
Deleted and recreated jobs, job definitions and repositories.
I hope this is enough detail to help me troubleshoot the issue.
Thanks for reading.
2
Answers
I reached a solution. Despite using
--no-cache
in the build command, local layers were apparently cached and after I randocker system prune -a
, built again and inspected the build, it was in the correct architecture, amd64. I pushed to the repository and it ran correctly.This error is typical of mismatched CPU architectures, so yes your assumption is correct.
Your command to build the container image for Intel/AMD 64-bit is correct. I suspect that you are using a mismatched container image tag when you’re attempting to deploy the container to the AWS Batch service. Make sure that when you run the
inspect
command, you are referencing the same tag as thebuild
command, instead of using thelatest
tag every time. That’s probably why you’re seeing inconsistent results.