How can I get the Docker target platform inside the Build Environment / Dockerfile
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.