4

I'm trying to get Sonar Scanner to analyze a project as part of a dockerfile using the mcr.microsoft.com/dotnet/core/sdk:3.1-nanoserver-1809 image, but it's throwing the following exception when attempting to run dotnet-sonarscanner begin

Unhandled exception. System.IO.IOException: Cannot find local application data directory.

I've verified that the folder it's looking for exists on the container with the following powershell command:

RUN pwsh -Command Get-ChildItem $env:LOCALAPPDATA -Directory

Not sure what I'm missing here. Here's a more complete picture of the dockerfile with example data:

FROM mcr.microsoft.com/dotnet/core/sdk:3.1-nanoserver-1809 AS build
WORKDIR /app

ARG SONAR_PROJECT_KEY=example-project-key
ARG SONAR_OGRANIZAION_KEY=exapmle-org-key
ARG SONAR_HOST_URL=https://sonarcloud.io
ARG SONAR_TOKEN

# install java
COPY ./install-java.ps1 .
RUN pwsh install-java.ps1

RUN cmd ver

# Install Sonar Scanner
RUN dotnet tool install --tool-path msbuildscanner dotnet-sonarscanner
#ENV PATH="$PATH:/root/.dotnet/tools"

RUN pwsh -Command Get-ChildItem $env:LOCALAPPDATA -Directory

# Start Sonar Scanner
RUN msbuildscanner\dotnet-sonarscanner begin /k:"$SONAR_PROJECT_KEY" /o:"$SONAR_OGRANIZAION_KEY" /d:sonar.host.url="$SONAR_HOST_URL" /d:sonar.login="$SONAR_TOKEN" 

# Copy csproj and restore as distinct layers
COPY *.csproj ./
RUN dotnet restore

# Copy everything else and build
COPY . ./
RUN dotnet publish -c Release -o out

RUN msbuildscanner\dotnet-sonarscanner end /d:sonar.login="$SONAR_TOKEN"

#Build runtime image
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1.9-nanoserver-1809 AS runtime
WORKDIR /app
COPY --from=build /app/out .

ENTRYPOINT ["dotnet", "app.dll"]
Jerreck
  • 2,930
  • 3
  • 24
  • 42
  • 1
    I'm running into this problem too. Have you had any luck @Jerreck ? – Chad Ruppert Jan 22 '21 at 19:38
  • Unfortunately not. Due to time constraints I wound up just abandoning this approach and instead having the sonar scan take place in a pipeline outside of the dockerfile. That means our pipelines build app code twice - once in the pipeline and again in docker build, but that's not a big performance hit for us atm. – Jerreck Jan 23 '21 at 07:35
  • That's basically what I'm going to end up doing too, I think. It's ridiculous what MS did to these 2019 based images.. and finding documentation on these changes has been difficult at best. Have you had the same experience? – Chad Ruppert Jan 25 '21 at 14:49

0 Answers0