NextJS Build Issues with Mac M1 and Docker Desktop

While developing a NextJS web application with Mac M1 and docker desktop, I faced a number of issues. This article summarises the problems and how I solved it.

The first issue I faced was during the docker container build from the NextJS folder with a Dockerfile. I used the following command to build the container image,

docker build . -t

During the docker build, the following error was observed when RUN npm install task was running,

=> [4/9] RUN npm install 324.9s
=> => # #
=> => # # Fatal process OOM in Failed to reserve virtual memory for CodeRange
=> => # #
=> => # qemu: uncaught target signal 5 (Trace/breakpoint trap) - core dumped

It turned out that this is an issue with Mac M1 machines if you are using older versions of docker desktop. Upgrading docker desktop to 4.5 solved the issue!

The next issue I faced was when I tried to deploy the docker image on an Azure AKS cluster running Linux VMS. The container failed to load on AKS with the CrashLoopBackOff error. The interesting thing was that this container image ran without any issues on my local Mac M1. I ran the following kubectl command to find the root cause of this error in AKS,

kubectl --v=8 logs myapp-bc12b6bb2-chbrg

The above command showed the following error text,

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

Then I realised that Mac M1 is generating container images suitable for ARM architecture! So to force creation of a docker images suitable for linux, I used the following modified command to create docker images,

docker build --platform=linux/amd64 . -t

This solved the issue and my NextJS app was running without any issues on AKS cluster.