Skip to content

Instantly share code, notes, and snippets.

@developer-guy
Created November 23, 2020 08:07
Show Gist options
  • Save developer-guy/cc579e01bca14fc8dfb7f4037436f44f to your computer and use it in GitHub Desktop.
Save developer-guy/cc579e01bca14fc8dfb7f4037436f44f to your computer and use it in GitHub Desktop.
cross-build-dockerfile-with-args
$ cat <<EOF >> Dockerfile
FROM alpine
ARG TARGETPLATFORM
ARG TARGETOS
ARG TARGETARCH
ARG TARGETVARIANT
ARG BUILDPLATFORM
ARG BUILDOS
ARG BUILDARCH
ARG BUILDVARIANT
RUN echo "Image target platform details :: "
RUN echo "TARGETPLATFORM : $TARGETPLATFORM"
RUN echo "TARGETOS : $TARGETOS"
RUN echo "TARGETARCH : $TARGETARCH"
RUN echo "TARGETVARIANT : $TARGETVARIANT"
RUN echo "Image build platform details :: "
RUN echo "BUILDPLATFORM : $BUILDPLATFORM"
RUN echo "BUILDOS : $BUILDOS"
RUN echo "BUILDARCH : $BUILDARCH"
RUN echo "BUILDVARIANT : $BUILDVARIANT"
EOF
$ docker buildx create --name my-new-builder --driver docker-container --use
$ docker buildx inspect --bootstrap
$ docker buildx build --platform=linux/amd64,linux/arm64,linux/s390x --push -t <dockerhub_id>/my-image:latest .
@developer-guy
Copy link
Author

When building an image with multiple platform targets, you might in some cases require to know what the building platform is and what the target platform is, there are a few arguments that are injected when running buildx build, and those are the following:

TARGETPLATFORM # Platform of the target, for example: 'linux/arm64' or `linux/arm/v7` 
TARGETOS       # Os part of the target platform, for example: 'linux' or 'windows'
TARGETARCH     # Arch part of the target platform, for example 'amd64', 'arm64' or 'arm'
TARGETVARIANT  # If the target platform have a variant (`v7` in arm for example), this will be set to that value
BUILDPLATFORM  # The platform of the build machine
BUILDOS        # OS of the build machine
BUILDARCH      # Architecture of the build machine
BUILDVARIANT   # Variant (if exist) of the build machine

As usual, you should define those with the ARG directive in your image, else they will not be available to use.

Link: https://jite.eu/2019/10/3/multi-arch-docker/

@developer-guy
Copy link
Author

Screen Shot 2020-11-23 at 11 12 14

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment