Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker Image built on Mac OSX won't run on AWS EC2 instance

Image built on Mac OSX with M1 processor, deployed to an EC2 instance. But when scripts are run it yields the error:

standard_init_linux.go:219: exec user process caused: exec format error

Elsewhere on Stackoverflow, this is explained as a mismatch of OS architecture. Sure enough running "uname -m" on EC2 instance shows it to be x86_64, and "docker image inspect" shows the container to have architecture arm64.

Here's what I don't understand. "uname -m" on my Mac shows that to be x86_64 too. So how does the container inherit a different architecture?

More significantly, how do I build an image on my Mac that I can run on EC2?

Docker file is simply

FROM python
WORKDIR /
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY src /src

with src containing, currently, some simple python scripts, executed thus:

docker run container/name python test.py

This works fine on my Mac, but gives the error above when executed on AWS.

like image 474
petercoles Avatar asked Jan 28 '21 17:01

petercoles


3 Answers

OK. Here's what's happening. My Mac has the new M1 chip and I'm running the Tech Preview version of Docker Desktop. Under the hood the chip has the arm64 architecture, but interrogating it through iTerm and VSCode it claims to be x86_64 instead, hence my confusion when I posted the question. This is probably because both those apps are being quietly run through an Intel simulator behind the scenes and that's what's responding to the uname command.

However, because the processor is really arm64, that's the base architecture when I pull Python images from Docker (I tried lots of different flavours nd version of Python - all with the same results).

To force use of an amd64 AWS-compatible image I changed the first line of the Dockerfile to:

FROM --platform=linux/x86-64 python.

When containers from this image are run on the Mac that causes a warning

WARNING: The requested image's platform (linux/amd64) does not match the detected host platform (linux/arm64/v8) and no specific platform was requested

but it's just that, a warning, and the script runs (presumably by redirecting back through the Intel simulator. The scripts now run without problem (or warning) on the EC2 instance.

like image 187
petercoles Avatar answered Sep 21 '22 02:09

petercoles


I'm not sure why you're getting this error, but there is a nice way to get around it if you'd like and if you don't mind your code and images being public. I'm guessing that this is just home-stuff anyway, so it might not be too bad.

  1. Put your code in github.
  2. Configure a repository on hub.docker.com for your image and configure automatic builds from github
  3. ssh onto your ec2 instance and pull your image directly from docker hub

An alternative is to start with step 1, then log into your ec2 using ssh and clone the repo on that machine. You can then build it directly on a real linux machine (your osx machine doesn't run Linux, which is an instant mismatch with docker). If you build it on the server you should be able to run it there with no problems.

like image 20
Software Engineer Avatar answered Sep 20 '22 02:09

Software Engineer


Try to run with CMD ["lscpu"] or something related like cat /proc/cpuinfo in the container, compare architectures

Another thing: you might be pulling arm architecture of python image when building, and try to run it on x86_64 (EC2)

like image 37
Telinov Dmitri Avatar answered Sep 24 '22 02:09

Telinov Dmitri