Answers: <p>I found the solution by trying various problems, and my issue was that the scope of the <code>ARG</code> command was wrong.</p>
  <p>I had in my <code>Dockerfile</code>:</p>
  <pre><code>FROM base_image_name AS build
  ARG BuildNumber=0.1.0.0
  
  # ...
  
  FROM build AS publish
  
  # ...
  
  RUN echo Build Number: ${BuildNumber}
  </code></pre>
  <p>instead, what fixed the issue on the Azure was:</p>
  <pre><code>FROM base_image_name AS build
  
  # ...
  
  FROM build AS publish
  ARG BuildNumber=0.1.0.0
  
  # ...
  
  RUN echo Build Number: ${BuildNumber}
  </code></pre>
  <p>I do not know why this built on my local machine, but failed on Azure, but with the scoping fixed it now builds.</p>
  
Answers: CC BY-SA 4.0
Answers: 2022-07-14T13:51:27.457Z
Answers: 2022-07-14T13:51:27.457Z
Answers: Jake Conkerton-Darby
Body: <p>I'm running an azure build pipeline for a dockerized application set up using an <code>azure-pipelines.yml</code> file. The problem is that we require a build arg passed through to the docker build command, but the arg is blank in all circumstances (even if no build arg is provided and a default is specified in the <code>Dockerfile</code>).</p>
  <p>Snippet of <code>azure-pipelines.yaml</code> (sensitive data changed only):</p>
  <pre class="lang-yaml prettyprint-override"><code>- task: Docker@2
    displayName: build
    inputs:
      containerRegistry: our_container_registry
      repository: $(Build.Repository.Name)
      command: build
      Dockerfile: '**/Dockerfile'
      tags: $(Build.BuildNumber)
      arguments: --build-arg BuildNumber=$(Build.BuildNumber)
  </code></pre>
  <p>Further details:</p>
  <ul>
  <li>We are using the VM image of <code>ubuntu-latest</code> to run the job</li>
  <li>The build arg appears to be being passed through correctly as it does appear in the printout of the command that is run
  <ul>
  <li>e.g. if we pass <code>--build-arg BuildNumber=1.2.3</code> as part of the <code>arguments</code> property of the yaml file, we'll see <code>--build-arg BuildNumber=1.2.3</code> on the printout of the command run.</li>
  </ul>
  </li>
  <li>As can be seen from the above snippet we're not running the <code>buildAndPush</code> command that I am aware has issues with build args (publish is it's own step we're not even getting to yet).</li>
  <li>The <code>Dockerfile</code> contains the command <code>RUN echo Build Number: ${BuildNumber}</code> which prints out <code>Build Number: </code> during the build.</li>
  <li>Running the same build command printed out by the build step locally builds the container with no errors, and prints out the build number appropriately in the above mentioned command</li>
  </ul>
  <p>I'm looking for an explanation as to how I can get the build args passed through correctly to our docker build, or if there is something we're not aware of preventing this then recommendations of other alternatives that would achieve the same ends.</p>
  
Tags: <docker><azure-pipelines>
Title: Azure pipelines Docker@2 build command does not pass through build args
UserDisplayName: Jake Conkerton-Darby
ThreadText: # Azure pipelines Docker@2 build command does not pass through build args
  
  (Asked by: username_0 on 2022-07-14)
  
  I'm running an azure build pipeline for a dockerized application set up using an `azure-pipelines.yml` file. The problem is that we require a build arg passed through to the docker build command, but the arg is blank in all circumstances (even if no build arg is provided and a default is specified in the `Dockerfile`).
  
  Snippet of `azure-pipelines.yaml` (sensitive data changed only):
  
  ```yaml
  - task: Docker@2
    displayName: build
    inputs:
      containerRegistry: our_container_registry
      repository: $(Build.Repository.Name)
      command: build
      Dockerfile: '**/Dockerfile'
      tags: $(Build.BuildNumber)
      arguments: --build-arg BuildNumber=$(Build.BuildNumber)
  
  ```
  
  Further details:
  
  * We are using the VM image of `ubuntu-latest` to run the job
  * The build arg appears to be being passed through correctly as it does appear in the printout of the command that is run
    + e.g. if we pass `--build-arg BuildNumber=1.2.3` as part of the `arguments` property of the yaml file, we'll see `--build-arg BuildNumber=1.2.3` on the printout of the command run.
  * As can be seen from the above snippet we're not running the `buildAndPush` command that I am aware has issues with build args (publish is it's own step we're not even getting to yet).
  * The `Dockerfile` contains the command `RUN echo Build Number: ${BuildNumber}` which prints out `Build Number:`  during the build.
  * Running the same build command printed out by the build step locally builds the container with no errors, and prints out the build number appropriately in the above mentioned command
  
  I'm looking for an explanation as to how I can get the build args passed through correctly to our docker build, or if there is something we're not aware of preventing this then recommendations of other alternatives that would achieve the same ends.
  
  **Tags:** docker, azure-pipelines
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2022-07-14. Score: 0
  
  I found the solution by trying various problems, and my issue was that the scope of the `ARG` command was wrong.
  
  I had in my `Dockerfile`:
  
  ```
  FROM base_image_name AS build
  ARG BuildNumber=0.1.0.0
  
  # ...
  
  FROM build AS publish
  
  # ...
  
  RUN echo Build Number: ${BuildNumber}
  
  ```
  
  instead, what fixed the issue on the Azure was:
  
  ```
  FROM base_image_name AS build
  
  # ...
  
  FROM build AS publish
  ARG BuildNumber=0.1.0.0
  
  # ...
  
  RUN echo Build Number: ${BuildNumber}
  
  ```
  
  I do not know why this built on my local machine, but failed on Azure, but with the scoping fixed it now builds.
  
  
  ---
  
  