Docker image on M1 Mac
Building Docker Images on Apple Silicon with buildx

Docker image on M1 Mac

Creating Docker images compatible across different architectures, especially when moving to Apple Silicon M1 chip MacBook, presents challenges. The default behavior of Docker on the M1 MacBook is to create linux/arm64 images, which align with ARM architecture. This creates a significant compatibility issue with Intel-based machines that use AMD architecture, as well as with cloud Kubernetes clusters like those on GCP or AWS.

A common error encountered in this scenario is typically occurring entry point error when running images built for ARM architecture on non-ARM systems. This emphasizes the need for a careful approach to Docker image creation, ensuring compatibility across different platforms.

exec /docker-entrypoint.sh: exec format error,         

However, the M1 chip MacBook supports linux/amd64 images, suggesting the solution lies in building Docker images specifically for this architecture.

The message includes an example Dockerfile for a React application to illustrate this point. The build command is modified to include the --platform linux/amd64 flag, ensuring the right platform compatibility.

# build environment
FROM --platform=linux/amd64 node:20.11.0 as builder
RUN mkdir /usr/src/app
WORKDIR /usr/src/app
ENV PATH /usr/src/app/node_modules/.bin:$PATH
COPY package.json /usr/src/app/package.json
RUN npm install --silent
RUN npm install [email protected] -g --silent
COPY . /usr/src/app
RUN npm run build

# production environment
FROM nginx:latest
COPY --from=builder /usr/src/app/build /usr/share/nginx/html
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]        

Now the command to build should add --platform flag with the correct platform that you want to build the docker image for.

docker buildx build --platform=linux/amd64 -t awsm-ui .         

Thank you for reading my article! For more updates and useful information, feel free to connect with me on LinkedIn and follow me on Twitter. I look forward to engaging with more like-minded professionals and sharing valuable insights.



要查看或添加评论,请登录

Nitin Rachabathuni的更多文章

社区洞察

其他会员也浏览了