Search code examples
.netdockerjenkinsasp.net-corebuild

Dotnet build permission denied in Docker container running Jenkins


I am trying to build a .NET application using Jenkins. The Jenkins instance is running in a Docker container.

My Jenkinsfile is as follows:

pipeline {
  agent {
    docker {
      image 'microsoft/dotnet:2.1-sdk'
      registryUrl 'https://index.docker.io/v1/'
    }
  }
  stages {
    stage('Build') {
      steps {
        sh 'dotnet build MyApplication/Application.csproj -c Release -o /app'
      }
    }
    stage('Test') {
      steps {
        sh 'dotnet test MyApplication/Application.csproj -c Release -r /results'
      }
    }
  }
}

When I attempt the build I am seeing the following error in the Jenkins build output:

System.UnauthorizedAccessException: Access to the path '/.dotnet' is denied. ---> System.IO.IOException: Permission denied
   --- End of inner exception stack trace ---
   at System.IO.FileSystem.CreateDirectory(String fullPath)
   at System.IO.Directory.CreateDirectory(String path)
   at Microsoft.Extensions.EnvironmentAbstractions.DirectoryWrapper.CreateDirectory(String path)
   at Microsoft.DotNet.Configurer.FileSentinel.Create()
   at Microsoft.DotNet.Configurer.DotnetFirstTimeUseConfigurer.Configure()
   at Microsoft.DotNet.Cli.Program.ConfigureDotNetForFirstTimeUse(INuGetCacheSentinel nugetCacheSentinel, IFirstTimeUseNoticeSentinel firstTimeUseNoticeSentinel, IAspNetCertificateSentinel aspNetCertificateSentinel, IFileSentinel toolPathSentinel, Boolean hasSuperUserAccess, DotnetFirstRunConfiguration dotnetFirstRunConfiguration, IEnvironmentProvider environmentProvider)
   at Microsoft.DotNet.Cli.Program.ProcessArgs(String[] args, ITelemetry telemetryClient)
   at Microsoft.DotNet.Cli.Program.Main(String[] args)

It appears that the '.dotnet' folder is protected in the Docker container. Is there a way of getting read/write permission on this, or a way of changing it's location? I can't seem to find the folder when I bash into the container.

Thanks for any help.


Solution

  • The issue appeared to be linked to trying to write data to the top level of the Docker container ('/').

    Adding the following to the Jenkinsfile ensures that the home directory is set and the .dotnet folder can be created in a location with correct permissions.

    environment {
       HOME = '/tmp'
    }