Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I solve COPY failed: file not found in build context or excluded in Azure Pipeline?

I got stuck very interesting issue with azure pipeline. The issue is "Forbidden path outside the build context".If you have an add line in the Dockerfile which points to another directory, the build of the image fails with the message "Forbidden path".How can I solve this?

I was recently attempting to Dockerize a C# project, so I added a docker folder to the project and created a simple Dockerfile to get started like below:

Error:

f83e9c616794: Pulling fs layer
9887694812e5: Pulling fs layer
9887694812e5: Verifying Checksum
9887694812e5: Download complete
dd4da9d953fb: Verifying Checksum
dd4da9d953fb: Download complete
f83e9c616794: Verifying Checksum
f83e9c616794: Download complete
dd4da9d953fb: Pull complete
f83e9c616794: Pull complete
9887694812e5: Pull complete
Digest: sha256:85ea9832ae26c70618418cf7c699186776ad066d88770fd6fd1edea9b260379a
Status: Downloaded newer image for mcr.microsoft.com/dotnet/sdk:5.0
 ---> bd73c72c93a1
Step 5/25 : WORKDIR /src
 ---> Running in b457b1934a7e
Removing intermediate container b457b1934a7e
 ---> a50c5df6f929
Step 6/25 : COPY ["./src/xxxxx.Web/xxxxx.Web.csproj", "src/xxxxx.Web/"]
COPY failed: file not found in build context or excluded by .dockerignore: stat src/xxxxx.Web/xxxxx.Web.csproj: file does not exist
##[error]COPY failed: file not found in build context or excluded by .dockerignore: stat src/xxxxx.Web/xxxxx.Web.csproj: file does not exist
##[error]The process '/usr/bin/docker' failed with exit code 1
Finishing: Build and push an image to container registry

DockerFiles/Dockerfile.xxxxx.Web:

FROM mcr.microsoft.com/dotnet/aspnet:5.0 AS base
WORKDIR /app
EXPOSE 80

FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build
WORKDIR /src
COPY ["src/xxxxx.Web/xxxxx.Web.csproj", "src/xxxxx.Web/"]
RUN dotnet restore "src/xxxxx.Web/xxxxx.Web.csproj"
COPY . .
WORKDIR "/src/src/xxxxx.Web"
RUN dotnet build "xxxxx.Web.csproj" -c Release -o /app

FROM build AS publish
RUN dotnet publish "xxxxx.Web.csproj" -c Release -o /app

FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "xxxxx.Web.dll"]

azure-pipeline.yml:

 # Deploy to Azure Kubernetes Service
# Build and push image to Azure Container Registry; Deploy to Azure Kubernetes Service
# https://learn.microsoft.com/azure/devops/pipelines/languages/docker

trigger:
- test


variables:

  # Container registry service connection established during pipeline creation
  dockerRegistryServiceConnection: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
  imageRepository: 'xxxxxxxx'
  containerRegistry: 'xxxxxxxxxxxx.azurecr.io'
  dockerfilePath: '**/DockerFiles/Dockerfile.xxxxx.Web'
  tag: '$(Build.BuildNumber)'

  # Agent VM image name
  vmImageName: 'ubuntu-latest'


stages:
- stage: Build
  displayName: Build stage
  jobs:
  - job: Build
    displayName: Build
    pool:
      vmImage: $(vmImageName)
    steps:
    - task: Docker@2
      displayName: Build and push an image to container registry
      inputs:
        command: buildAndPush
        repository: $(imageRepository)
        dockerfile: $(dockerfilePath)
        containerRegistry: $(dockerRegistryServiceConnection)
        tags: |
          $(tag)

    - publish: manifests
      artifact: manifests

- stage: Deploy
  displayName: Deploy stage
  dependsOn: Build

  jobs:
  - deployment: Deploy
    displayName: Deploy-xxxxx.Web
    pool:
      vmImage: $(vmImageName)
    environment: 'xxxxx-5982.default'
    strategy:
      runOnce:
        deploy:
          steps:


          - task: KubernetesManifest@0
            displayName: Deploy to Kubernetes cluster
            inputs:
              action: deploy
              manifests: |
                $(Pipeline.Workspace)/manifests/deployment.eventhub.web.yml
                $(Pipeline.Workspace)/manifests/service.eventhub.web.yml
              containers: |
                $(containerRegistry)/$(imageRepository):$(tag)

How can I solve "COPY failed: Forbidden path outside the build context"? I want to use Dockerfile in DockerFiles Directory.

like image 656
ALEXALEXIYEV Avatar asked Apr 28 '21 07:04

ALEXALEXIYEV


People also ask

How do I put files outside Docker's build context?

The best way to work around this is to specify the Dockerfile independently of the build context, using -f. For instance, this command will give the ADD command access to anything in your current directory. docker build -f docker-files/Dockerfile . docker build -f ../Dockerfile .

Where is $( build SourcesDirectory?

$(Build. SourcesDirectory) : The local path on the agent where your source code files are downloaded. For example: c:\agent_work\1\s By default, new build definitions update only the changed files. You can modify how files are downloaded on the Repository tab.

What happened to azcopy in azure pipelines V4?

The new v4 of the File Copy Task in Azure Pipelines moved from using AzCopy 8 to AzCopy 10, and with all major updates comes with breaking changes. Both old and new use an Azure DevOps Service Principal to authenticate with Azure, but security is tighter on v4. I assume this has something to do with updating the version of AzCopy.

Why do I get a build context error in pipeline task?

The error occurs when the pipeline task build context does not match the build context defined in the Dockerfile. By default it assumes the Dockerfile directory which is not always the case for Visual Studio generated solutions/projects.

How to solve copy failed error in dockerfile?

Try to set the Dockerfile in the root of the solution should solve your error. Also you need to add a buildContext with the csproj folder. Also, here is a case you can refer to. One more possible cause of COPY failed: no source files were specified is .dockerignore file present in your workspace

What's new in azure pipelines V4?

The new v4 of the File Copy Task in Azure Pipelines moved from using AzCopy 8 to AzCopy 10, and with all major updates comes with breaking changes. Both old and new use an Azure DevOps Service Principal to authenticate with Azure, but security is tighter on v4.


Video Answer


2 Answers

You can try to add buildContext parameter in the Docker task.

buildContext: Path to the build context

Here is a case you can refer to.

like image 186
Hugh Lin Avatar answered Oct 23 '22 14:10

Hugh Lin


In my case Build Context, uncheck the Use Default Build Context and set the Project Solution folder. Refer image build context set

like image 1
Karunakaran Avatar answered Oct 23 '22 14:10

Karunakaran