skip to Main Content

I want to build the dockerfile of the .net service in my repo using Azure Pipeline. The folder structure is as follows:

|-- ProjectA  
|-- |-- RepoB  
|       | -- .gitignore  
|       | -- Example.sln  
|       | -- Readme.md  
|       | -- Services  
|                 | -- ServiceC  
|                      | -- ServiceC.API  
|                           | -- Program.cs   
|                           | --     .  
|                           | --     .  
|                           | -- Dockerfile  
|                 | -- ServiceD  
|                      | -- ServiceD.API  
|                           | -- Program.cs   
|                           | --     .  
|                           | --     .  
|                           | -- Dockerfile    

When I try to build the above dockerfiles with Azure pipeline, I get the following error::

[error]Unhandled: No Dockerfile matching /home/vsts/work/1/s/Dockerfile was found.

azure-pipelines.yml content as follows:

# Docker
# Build a Docker image
# https://docs.microsoft.com/azure/devops/pipelines/languages/docker

trigger:
- test

resources:
- repo: self

variables:
  tag: '$(Build.BuildId)'

stages:
- stage: Build
  displayName: Build image
  jobs:
  - job: Build
    displayName: Build
    pool:
      vmImage: ubuntu-latest
    steps:
    - task: Docker@2
      displayName: Build an image for Lesson Service
      inputs:
        command: build
        buildContext: 'Services/ServiceC/ServiceC.API/**'
        dockerfile: 'Dockerfile'
        tags: |
          $(tag)

Can you help me solve the error? Thanks for your help.

Yaml file can be run and find the Dockerfile

2

Answers


  1. Ok easy one,

    So you have two parameters

        buildContext: 'Services/ServiceC/ServiceC.API/**'
        dockerfile: 'Dockerfile'
    

    Build context is where the build is going to essentially be built from and dockerfile is the location of the docker file.

    When your pipeline gets to that task you will be executing from /home/vsts/work/1/s/ so your path to the docker file will be as below, if you a dropping the whole repo.

    ProjectA/RepoB/Services/ServiceC/ServiceC.API/Dockerfile

    But I am going to assume ProjectA is the Team Project and RepoB is the name of the git/TFVC repo, so not use is the pipeline folder drop.

    Services/ServiceC/ServiceC.API/Dockerfile

    Location on the build agent would be

    /home/vsts/work/1/s/Services/ServiceC/ServiceC.API/Dockerfile

    I wouldn’t use wildcards in the buildcontext, I always set my build context at solution level, but it all depends on how you dockerfile is constructed.

    - task: Docker@2
          displayName: Build an image for ServiceC
          inputs:
            command: build
             buildContext: 'Services/Services/ServiceC'
            dockerfile: 'Services/ServiceC/ServiceC.API/Dockerfile'
            tags: |
              $(tag)
    

    You will then second task for the next dockerfile

    - task: Docker@2
          displayName: Build an image for ServiceD 
          inputs:
            command: build
             buildContext: 'Services/Services/ServiceD'
            dockerfile: 'Services/ServiceD/ServiceD.API/Dockerfile'
            tags: |
              $(tag)
    

    If you want both Services in a single container this is not the way to do it, but that is totally separate question.

    Login or Signup to reply.
  2. I can reproduce the same with the folder structure and yaml:
    enter image description here

    To fix the error, you need to remove buildContext parameter, and define file path for dockerfile.

        steps:
        - bash: cat Services/ServiceC/ServiceC.API/Dockerfile
        - task: Docker@2
          displayName: Build an image for Lesson Service
          inputs:
            command: build
            dockerfile: 'Services/ServiceC/ServiceC.API/Dockerfile'
            tags: |
              $(tag)
    

    The pipeline works then:

    enter image description here

    The buildContext is useful when you have containerRegistry defined.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search