Answers: <p>One other option is to use <code>TARGETPLATFORM</code> as mentioned but with multistage Dockerfile.</p>
  <p>If there's no complex logic apart from a few different steps (between targets), this allows to keep everything in the Dockerfile (and a single one).</p>
  <pre><code>FROM alpine:3.16 as main-linux-armv7
  # do whatever is different on linux/arm/v7
  
  FROM alpine:3.16 as main-linux-amd64
  # do whatever should be for linux-amd64
  
  FROM main-linux-amd64 as main-linux-arm64
  # linux-arm64 is the same as linux-amd64 but every target needs to be defined
  
  FROM main-${TARGETOS}-${TARGETARCH}${TARGETVARIANT}
  # main / final stage
  </code></pre>
  
Answers: CC BY-SA 4.0
Answers: 2022-07-17T10:13:57.227Z
Answers: 2022-07-17T10:13:57.227Z
Answers: Filipe Pina
Answers: <p>The simplest answer will be to use <code>TARGETPLATFORM</code> variable which is available when you run <code>docker buildx build</code> ... command. Only thing to remember is that for any variable that is available you need to declare it using <code>ARG</code>.</p>
  <p>So, a sample <code>Dockerfile</code> snippet can be:</p>
  <pre><code>ARG TARGETPLATFORM
  RUN if [ &quot;$TARGETPLATFORM&quot; = &quot;linux/arm64&quot; ]; then \
          echo &quot;ARM64 version&quot;; \
      else \
          echo &quot;AMD64 version&quot;; \
      fi
  </code></pre>
  
Answers: CC BY-SA 4.0
Answers: 2024-11-19T15:16:50.420Z
Answers: 2024-11-20T16:13:33.017Z
Answers: 2024-11-20T16:13:33.017Z
Answers: rkdove96
Answers: <p>I would just have a base script that have the things in common for most of the platform like base_install.sh</p>
  <p>Then I would have multiple dockerfiles that can use this base script, an example for your amd64 dockerfile DockerfileAMD64:</p>
  <pre><code>FROM mybase64image
  
  RUN ./base_install.sh
  RUN apt-get install specific64things -y
  </code></pre>
  <p>and you can build it with:</p>
  <pre><code>docker build -f DockerfileAMD64
  </code></pre>
  <p>That way you can version control these files without duplicating code between the dockerfiles.</p>
  
Answers: CC BY-SA 4.0
Answers: 2021-03-11T21:49:06.203Z
Answers: I'm specifically using buildx for multi-platform builds, and a single Dockerfile for the service in question, what you propose would greatly complicate things for the need of a single requisite that isn't available on arm platforms.
  
  For now, I'm actually using...
  
      RUN ... apt install -yqq ... && \
          (apt install -yqq dosemu || echo "No dosemu for platform") && \
Answers: Tracker1
Answers: CC BY-SA 4.0
Answers: 2021-03-01T15:15:12.880Z
Answers: 2021-03-01T15:21:11.370Z
Answers: 2021-03-01T15:21:11.370Z
Answers: Renato Prado
Answers: <p>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 <code>RUN</code> command, but it risks becoming quite complex and difficult to read in the Dockerfile. Furthermore, each additional <code>RUN</code> or <code>CMD</code> entry in the Dockerfile adds a layer to the image, which is <a href="https://docs.docker.com/develop/develop-images/dockerfile_best-practices/#minimize-the-number-of-layers" rel="nofollow noreferrer">considered bad practice</a>.</p>
  <p>This script can instead take the arguments <code>BUILDPLATFORM</code> and <code>TARGETPLATFORM</code> as suggested in <a href="https://docs.docker.com/buildx/working-with-buildx/#build-multi-platform-images" rel="nofollow noreferrer">the buildx docs</a>, 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.</p>
  
Answers: CC BY-SA 4.0
Answers: 2021-03-11T21:53:25.430Z
Answers: Thank you... BUILDPLATFORM and TARGETPLATFORM are precisely what I was looking for.  As to an extra RUN entry, it would be integrated into a chain command for prerequisites from apt... I'm currently doing `&& (apt install -yqq dosemu || echo "dosemu not available for platform") \` in the chain separate from the other apt tools.  It's a little more involved than I would like, but is at least working for now.  https://github.com/bbs-io/synchronet-docker/blob/master/docker/Dockerfile
Answers: Tracker1
Answers: CC BY-SA 4.0
Answers: 2021-03-11T21:54:22.767Z
Answers: I know the Dockerfile has more steps than ideal, was more concerned about correctness initially than reducing steps.
Answers: Tracker1
Answers: CC BY-SA 4.0
Answers: 2021-02-28T06:52:59.090Z
Answers: 2021-02-28T06:52:59.090Z
Answers: Bruce Becker
Body: <p>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.</p>
  <p>For example, something like...</p>
  <pre><code># No DOSEMU outside amd64
  RUN if [ &quot;$DOCKER_PLATFORM&quot; = &quot;amd64&quot; ]; then \
        apt install dosemu \
      fi
  </code></pre>
  
Tags: <docker><dockerfile><bash>
Title: How can I get the Docker target platform inside the Build Environment / Dockerfile
UserDisplayName: Tracker1
ThreadText: # How can I get the Docker target platform inside the Build Environment / Dockerfile
  
  (Asked by: username_0 on 2021-02-27)
  
  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
  
  ```
  
  **Tags:** docker, dockerfile, bash
  
  
  ---
  
  ## 4 answers
  
  ### Answer by username_4 on 2021-02-28. Score: 4 | Accepted answer
  
  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](https://docs.docker.com/develop/develop-images/dockerfile_best-practices/#minimize-the-number-of-layers).
  
  This script can instead take the arguments `BUILDPLATFORM` and `TARGETPLATFORM` as suggested in [the buildx docs](https://docs.docker.com/buildx/working-with-buildx/#build-multi-platform-images), 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.
  
  > **Comments:**
  > **username_0**: Thank you... BUILDPLATFORM and TARGETPLATFORM are precisely what I was looking for.  As to an extra RUN entry, it would be integrated into a chain command for prerequisites from apt... I'm currently doing `&& (apt install -yqq dosemu || echo "dosemu not available for platform") \` in the chain separate from the other apt tools.  It's a little more involved than I would like, but is at least working for now.  https://github.com/bbs-io/synchronet-docker/blob/master/docker/Dockerfile  
  > **username_0**: I know the Dockerfile has more steps than ideal, was more concerned about correctness initially than reducing steps.  
  
  ---
  
  ### Answer by username_1 on 2022-07-17. Score: 2
  
  One other option is to use `TARGETPLATFORM` as mentioned but with multistage Dockerfile.
  
  If there's no complex logic apart from a few different steps (between targets), this allows to keep everything in the Dockerfile (and a single one).
  
  ```
  FROM alpine:3.16 as main-linux-armv7
  # do whatever is different on linux/arm/v7
  
  FROM alpine:3.16 as main-linux-amd64
  # do whatever should be for linux-amd64
  
  FROM main-linux-amd64 as main-linux-arm64
  # linux-arm64 is the same as linux-amd64 but every target needs to be defined
  
  FROM main-${TARGETOS}-${TARGETARCH}${TARGETVARIANT}
  # main / final stage
  
  ```
  
  
  ---
  
  ### Answer by username_2 on 2024-11-19. Score: 1
  
  The simplest answer will be to use `TARGETPLATFORM` variable which is available when you run `docker buildx build` ... command. Only thing to remember is that for any variable that is available you need to declare it using `ARG`.
  
  So, a sample `Dockerfile` snippet can be:
  
  ```
  ARG TARGETPLATFORM
  RUN if [ "$TARGETPLATFORM" = "linux/arm64" ]; then \
          echo "ARM64 version"; \
      else \
          echo "AMD64 version"; \
      fi
  
  ```
  
  
  ---
  
  ### Answer by username_3 on 2021-03-01. Score: 1
  
  I would just have a base script that have the things in common for most of the platform like base\_install.sh
  
  Then I would have multiple dockerfiles that can use this base script, an example for your amd64 dockerfile DockerfileAMD64:
  
  ```
  FROM mybase64image
  
  RUN ./base_install.sh
  RUN apt-get install specific64things -y
  
  ```
  
  and you can build it with:
  
  ```
  docker build -f DockerfileAMD64
  
  ```
  
  That way you can version control these files without duplicating code between the dockerfiles.
  
  > **Comments:**
  > **username_0**: I'm specifically using buildx for multi-platform builds, and a single Dockerfile for the service in question, what you propose would greatly complicate things for the need of a single requisite that isn't available on arm platforms.  For now, I'm actually using...      RUN ... apt install -yqq ... && \         (apt install -yqq dosemu || echo "No dosemu for platform") && \  
  
  ---
  
  