Azure pipelines Docker@2 build command does not pass through build args



  • 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):

    - 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.


  • QA Engineer

    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.




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2