Build Docker images for platform arm64 (Apple M1) and amd64 on Apple M1 with Docker Desktop.
Prerequisites
Docker environment has been set up, Docker Engine: 20.10.1
recommended,
and version 4.3.0 (71786)
for Docker Desktop is recommended as it almost natively supports Apple M1.
Install Docker Buildx plugin
# Download buildx plugin
curl -LO https://github.com/docker/buildx/releases/download/v0.7.1/buildx-v0.7.1.darwin-$(uname -m)
# Move buildx to cli-plugins and make it executable
mkdir -p ~/.docker/cli-plugins
mv buildx-v0.7.1.darwin-$(uname -m) ~/.docker/cli-plugins/docker-buildx
chmod a+x ~/.docker/cli-plugins/docker-buildx
Prepare the environment for building the images
Here the settings are for building amd64 and arm64
docker buildx create --driver-opt image=moby/buildkit:master --name builder --driver docker-container --use
docker buildx inspect --bootstrap
docker buildx create --platform linux/amd64
docker buildx create --platform linux/arm64,linux/arm/v8
Build and push to Docker hub
Steps:
- Login Docker Hub:
docker login
- Build and push to Docker hub:
docker buildx build — push -t TAG — platform linux/amd64,linux/arm64 -f Dockerfile .
About the tag
A tag for the image you need to replace TAG
. Note: The dot
at the end of the command is a part of the command. The format of the tag looks like docker_hub_id/image_name:version
. For example: wxwmatt/rails:6.1.4.1-alpine3.15
.
Put all commands together
curl -LO https://github.com/docker/buildx/releases/download/v0.7.1/buildx-v0.7.1.darwin-$(uname -m)
mkdir -p ~/.docker/cli-plugins
mv buildx-v0.7.1.darwin-$(uname -m) ~/.docker/cli-plugins/docker-buildx
chmod a+x ~/.docker/cli-plugins/docker-buildx
docker buildx create --driver-opt image=moby/buildkit:master --name builder --driver docker-container --use
docker buildx inspect --bootstrap
docker buildx create --platform linux/amd64
docker buildx create --platform linux/arm64,linux/arm/v8
docker login
docker buildx build --push -t docker_id/image:version --platform linux/amd64,linux/arm64 .