Search code examples
dockerdeployment64-bitaws-cdkarm64

How to build a docker image for AWS/x64 deployment on MacBook M2 arm64?


I recently received a MacBook Pro M2 and am running into issues when I cdk deploy from it. There's a numpy import issue specifically relating to its "C-extensions".

Original error was: No module named 'numpy.core._multiarray_umath.

I see thjat once cdk deploy kicks off, Docker Desktop has the very first layer as "ARCHITECTURE arm64". I'm guessing this is at least partly why the build runs into issues once it actually tries to run on the x64 AWS architecture. Am I plain unable to do a build like this locally?

I tried a few different versions of numpy to import. I deployed the same code from a coworker's x64 laptop and it ran successfully.

SOLVED
Going along the lines of the zessx' comment below I ended up solving the problem by adding export DOCKER_DEFAULT_PLATFORM="linux/amd64" to my zsh profile.


Solution

  • Docker will try to use image fitting your local architecture. The new Apple M1/M2 are using an ARM64 architecture, therefore Docker will search for ARM64 images, by default.

    In your case, when running cdk deploy you are probably building an image in the backend. This image will be build with an ARM64 arch, but is expected to run on an AMD64 arch.

    You have several ways to ensure you're building your images for the right arch.

    Use the --platform flag with your Docker commands

    docker build -t $IMAGE --platform linux/amd64 .
    docker push ${AWS_ACCOUNT}.dkr.ecr.${AWS_REGION}.amazonaws.com/${IMAGE}
    

    Use the --platform flag in your Dockerfiles

    FROM ruby:latest as development
    RUN …
    
    FROM --platform=linux/amd64 ruby:latest as production
    RUN …
    

    Then, you can specify which target to build in the Docker command:

    docker build -t $IMAGE --target production .
    

    Use the DOCKER_DEFAULT_PLATFORM environment variable

    export DOCKER_DEFAULT_PLATFORM="linux/amd64"
    

    You can also add this line in your .bashrc/.zshrc, but keep in mind you would then use AMD64 emulation all the time, even for your local projects, where native ARM64 images are available. I would not recommend this.