Search code examples
docker.net-coredocker-composemsbuildsonarscanner

SonarScanner for .NET in Docker - solution builds with `docker run`, but not with `docker-compose`


I am trying to make code analysis using SonnarScanner for .NET using docker with nosinovacao/dotnet-sonar image, however, I cannot build a project using docker-compose while everything's fine when I use docker run with the same parameters.

The solution consists of several sub-solutions, it seems that MS Build has some problems with building the whole solution when docker-compose is used, since I'm getting MSBUILD : error MSB1008: Only one project can be specified. error.

When docker run is used there's a message Determining projects to restore... and then the solution is built correctly.

I don't understand what's the issue since I use exactly the same image and parameters.

Here's my working docker run command:

docker run -it --rm \
   --network=host \
    -v /Developer/MyProject/:/source \
    nosinovacao/dotnet-sonar:latest \
    bash -c \
        "cd source \
        && dotnet /sonar-scanner/SonarScanner.MSBuild.dll begin \
        /k:MyProjectKey /name:MyProject /version:v1.2.3 \
        /d:sonar.host.url="http://localhost:9000/" \
        && dotnet build MyProject.sln -c Release \
        && dotnet /sonar-scanner/SonarScanner.MSBuild.dll end"

and docker-compose:

version: "3.8"
services:
  dotnetscanner:
    entrypoint: /bin/bash
    command: -c "
        cd source 
        && dotnet /sonar-scanner/SonarScanner.MSBuild.dll begin 
        /k:MyProjectKey /name:MyProject /version:v1.2.3 
        /d:sonar.host.url="http://localhost:9000/"
        && dotnet build MyProject.sln -c Release \
        && dotnet /sonar-scanner/SonarScanner.MSBuild.dll end"
    image: nosinovacao/dotnet-sonar:latest
    volumes:
      - "/Developer/MyProject/:/source"
    network_mode: host

The docker image uses SonarScanner for MSBuild 5.0.4 and MSBuild version 16.8.3+39993bd9d for .NET.

What can I do to make the SonarScanner working with docker-compose?

Thanks in advance.

EDIT: It seems that something is wrong with passing parameters, when I explicitly define non-existing project in docker-compose I'm getting:

dotnetscanner_1  | MSBUILD : error MSB1008: Only one project can be specified.
dotnetscanner_1  | Switch:  

When I do the same for docker run the Switch: is not null in an error message:

MSBUILD : error MSB1009: Project file does not exist.
Switch: NonExistingProject.csproj

Solution

  • It turned out that the multi-line arguments caused the problem.

    To fix this one can either make pass command arguments as a single line or use the following syntax:

    version: "3.8"
    services:
      dotnetscanner:
        command: >
         sh -c "cd source && dotnet /sonar-scanner/SonarScanner.MSBuild.dll begin /k:ProjectKey /name:Project /version:v1.2.2 
         /d:sonar.host.url="http://localhost:9000/" 
         && dotnet build -c Release  
         && dotnet /sonar-scanner/SonarScanner.MSBuild.dll end"
        image: nosinovacao/dotnet-sonar:latest
        volumes:
          - "/Project:/source"
        network_mode: host