2

I am building a pipeline in Azure DevOps and trying to create a job that builds a Docker image. My intention is that the step fails if the Docker build is failing but against my expectations the tasks still passes even though the build has errors.

Here is the part of my pipeline:

stages:
- stage: build
  displayName: Build
  jobs:
    - job: build_service
      displayName: Build Service
      dependsOn: []
      pool:
        vmImage: 'ubuntu-22.04'
      steps:
        - task: Docker@2
          displayName: Docker Build
          inputs:
            command: build
            addBaseImageData: false
            addPipelineData: false
            buildContext: .
            Dockerfile: $(Build.SourcesDirectory)/servic/Dockerfile

Because of the typo in $(Build.SourcesDirectory)/servic/Dockerfile the Dockerfile is not found. The build fails but the pipeline task is succeeding anyway. Here are the logs:

(node:2524) UnhandledPromiseRejectionWarning: Error: No Dockerfile matching  /home/vsts/work/1/s/servic/Dockerfile  was found.
    at Object.run (/home/vsts/work/_tasks/Docker_e28912f1-0114-4464-802a-a3a35437fd16/2.212.1/dockerbuild.js:15:15)
    at getToken.then (/home/vsts/work/_tasks/Docker_e28912f1-0114-4464-802a-a3a35437fd16/2.212.1/docker.js:72:27)
    at process._tickCallback (internal/process/next_tick.js:68:7)
    at Function.Module.runMain (internal/modules/cjs/loader.js:834:11)
    at startup (internal/bootstrap/node.js:283:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:623:3)
(node:2524) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:2524) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
Finishing: Docker Build

For me it looks like a bug in the Docker@2 task, but still wonder if I am missing some setting.

Daniel Lerps
  • 5,256
  • 3
  • 23
  • 33

1 Answers1

3

I could reproduce your issue. Then if you run the same command in local, you may meet the same error 'couldn't find the dockerfile'. The Docker@2 task here only type the error got from the execution of the command 'docker build --file ***' and not recognize it as a standard error for the task. That should be the cause why the task runs well.

My suggestion is to run the command directly use command line task such as Bash@3 task. It should be something like this:

- task: Bash@3
  inputs:
    targetType: 'inline'
    failOnStderr: true    #for the task has the parameter 'fail the task when there are standard error print, you could add it for a try'
    script: 'docker build --file ***'

Then it will fail if there are some errors on the task. I hope it could do some help. Much thanks.

Antonia Wu-MSFT
  • 499
  • 2
  • 4
  • Thanks. I ended up using a slightly different approach with an extra tasks that verifies the existance of the docker file. – Daniel Lerps Nov 18 '22 at 13:56
  • 1
    Hi @DanielLerps, thanks for your warm reply! And just as a reminder, you could post to share and mark your answer to help others who will meet the issue in the future. Much thanks. – Antonia Wu-MSFT Nov 22 '22 at 06:36