Skip to content

Instantly share code, notes, and snippets.

@gubatron
Last active November 28, 2022 17:56
Show Gist options
  • Save gubatron/4fcfdc93af971ca8087cb66b316c37f0 to your computer and use it in GitHub Desktop.
Save gubatron/4fcfdc93af971ca8087cb66b316c37f0 to your computer and use it in GitHub Desktop.
How to build your Docker image using the same Dockerfile regardless of the host architecture

How to build your Docker image using the same Dockerfile regardless of the host architecture

Problem

If you are now using docker on a Mac M1 (arm64 platform), you don't want to use amd64 as the architecture for your Linux Images.

You could have 2 lines on your Dockerfile and comment each one depending on where you're building the image

Dockerfile

# Building on Apple Silicon host
FROM --platform=linux/arm64 ubuntu:20.04

# Building on Intel/x86_64 host
#FROM --platform=linux/amd64 ubuntu:20.04

Eventually this becomes very annoying.

Solution

You can pass a build time argument when you invoke docker build

Put this on your Dockerfile:

ARG BUILDPLATFORM
FROM --platform=linux/$BUILDPLATFORM ubuntu:20.04

On your docker_build_image.sh script:

export BUILDPLATFORM=`uname -m`
docker build --build-arg BUILDPLATFORM=${BUILDPLATFORM} -t myimagename .
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment