DEV Community

Anil-Sunam-Darjee
Anil-Sunam-Darjee

Posted on

Docker Build with Mac

Docker is a popular containerization platform that allows developers to package their applications and dependencies into portable containers. This article provides a step-by-step guide on how to build Docker images on a Mac with an M1 chip.

Solution Steps

Docker features an additional build called 'buildx' that should be included with the Docker installation (ver:^20). We will use the buildx command to build the image.

Step 1

Open Docker Desktop and go to Settings > Resources tab. Increase memory and swap size, and restart Docker Desktop, so that later we do not run into the JavaScript heap out of memory error.
Resource setting

Step 2

Also, add and modify the following command in the node-project/Dockerfile.

OLD DOCKERFILE

### STAGE 1: Build ###
# We label our intermediary docker container as 'builder' to build/compile frontend dependencies
FROM node:10.15.0-alpine as builder
ARG environment
Enter fullscreen mode Exit fullscreen mode

NEW DOCKERFILE

### STAGE 1: Build ###
# We label our intermediary docker container as 'builder' to build/compile frontend dependencies
FROM --platform=$BUILDPLATFORM node:10.15.0-alpine as builder
ARG environment
ENV NODE_OPTIONS=--max_old_space_size=2048
Enter fullscreen mode Exit fullscreen mode

Step 3

Generally, the CPU of your machine can only run binaries for its native architecture. An x86 CPU can't run ARM binaries and vice versa. So, when we are building an image on an Intel machine, it cannot run the shell binary for ARM. Let's install the Docker image which contains binaries for other architectures.

 $ docker run --privileged --rm tonistiigi/binfmt --install all
Enter fullscreen mode Exit fullscreen mode

Step 4

Create a new builder using the docker-container driver which gives you access to more complex features like multi-platform builds and the more advanced cache exporters, which are currently unsupported in the default Docker driver:

$ docker buildx create --name customBuilder --driver docker-container --bootstrap
$ docker buildx use customBuilder
Enter fullscreen mode Exit fullscreen mode

Step 5

Build & load image locally

The command below will build an image for standard x86 platforms (linux/amd64). For Apple Silicon Macs, use the platform as (linux/arm64).

[Note: run the command from the project directory]

$ docker buildx build --platform=linux/amd64 -t <tag> --build-arg environment=<env> . --load
Enter fullscreen mode Exit fullscreen mode

Build & push image directly

$ docker buildx build --platform=linux/amd64 -t <tag> --build-arg environment=<env> . --push
Enter fullscreen mode Exit fullscreen mode

Top comments (0)