Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 240
colmulhall Avatar asked Nov 30 '18 11:11

colmulhall


People also ask

Can you run Jenkins in a container?

Launch Jenkins in a Docker container. Install additional tools and plugins. Pass Java system properties and Jenkins application arguments. Backup the Docker volume.


2 Answers

You can set the HOME environment variable as @colmulhall suggested, but then you will set the docker container home directory to /tmp.

To do it in "dotnet" way set the environment variable DOTNET_CLI_HOME:

environment {
    DOTNET_CLI_HOME = "/tmp/DOTNET_CLI_HOME"
}

Or before calling dotnet run:

export DOTNET_CLI_HOME="/tmp/DOTNET_CLI_HOME"


Update

A sample Jenkins pipeline code taken from https://www.jenkins.io/doc/pipeline/tour/environment/

Look how DOTNET_CLI_HOME is defined in the environment section:

pipeline {
    agent {
        label '!windows'
    }

    environment {
        DISABLE_AUTH = 'true'
        DB_ENGINE    = 'sqlite'
        DOTNET_CLI_HOME = "/tmp/DOTNET_CLI_HOME"
    }

    stages {
        stage('Build') {
            steps {
                echo "Database engine is ${DB_ENGINE}"
                echo "DISABLE_AUTH is ${DISABLE_AUTH}"
                sh 'printenv'
            }
        }
    }
}

There are many ways to achieve this. If you are using docker, maybe a better way is defining the environment variable DOTNET_CLI_HOME in the docker image.

like image 148
hpaknia Avatar answered Oct 12 '22 04:10

hpaknia


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'
} 
like image 6
colmulhall Avatar answered Oct 12 '22 03:10

colmulhall