HiveBrain v1.2.0
Get Started
← Back to all entries
snippetbashdockerMinor

How can I get the Docker target platform inside the Build Environment / Dockerfile

Submitted by: @import:stackexchange-devops··
0
Viewed 0 times
platformcanthetargetdockerenvironmentdockerfilegethowbuild

Problem

I'm doing multi-platform builds with buildx and would like to know if there is a way to determine what platform is currently being build so I can avoid certain steps for certain environments.

For example, something like...

# No DOSEMU outside amd64
RUN if [ "$DOCKER_PLATFORM" = "amd64" ]; then \
      apt install dosemu \
    fi

Solution

This is typically done with a script, not the Dockerfile. The script (perhaps in bash or python) can run at build time to determine what environment it is in, and act accordingly. This can be included in the RUN command, but it risks becoming quite complex and difficult to read in the Dockerfile. Furthermore, each additional RUN or CMD entry in the Dockerfile adds a layer to the image, which is considered bad practice.

This script can instead take the arguments BUILDPLATFORM and TARGETPLATFORM as suggested in the buildx docs, which would make more declarative. Instead of discovering the target, you would declare it (and write your script accordingly), so that the user understands exactly what happens without having to look inside the build.

Context

StackExchange DevOps Q#13446, answer score: 3

Revisions (0)

No revisions yet.