Home > Back-end >  When I execute docker command from C# app I get "docker:invalid reference format" error
When I execute docker command from C# app I get "docker:invalid reference format" error

Time:05-26

I've read all similar questions and found no answer. I have docker-command which perfectly works for me in Linux command bush (Ubuntu):

 docker run -it --rm \
        -v /home/source3/mySolution:/myProj \
        nosinovacao/dotnet-sonar:latest \
        bash -c \
            "cd myProj  \
            && dotnet /sonar-scanner/SonarScanner.MSBuild.dll begin \
            /k:testProj3 /name:testProj3 /version:1.0 \
            /d:sonar.host.url="http://192.168.1.98:9000" \
            /d:sonar.login="<myToken>" \
            && dotnet restore \
            && dotnet build -c Release \
            && dotnet /sonar-scanner/SonarScanner.MSBuild.dll end \
            /d:sonar.login="<myToken>""

It's Sonarcube scanner, which analyzes the project located in linux directory /home/source3/mySolution. Now I try to execute the same command by C# Console application. I tried to run application by Visual Studio and in Docker, the result is the same. My code is:

public void StartAnalysis(string projectName, string projectKey)
        {
            _logger.LogInformation($"{nameof(StartAnalysis)} called with params: {nameof(projectName)}: '{projectName}', {nameof(projectKey)}: '{projectKey}'");

            var command = @$"run -it --rm \
                          -v ""/home/source3/mySolution:/myProj"" \
                          nosinovacao/dotnet-sonar:latest \
                          bash -c \
                            ""cd myProj \
                            && dotnet /sonar-scanner/SonarScanner.MSBuild.dll begin \
                            /k:{projectKey} /name:{projectName} /version:1.0 \
                            /d:sonar.host.url=""{_baseUrl}"" \
                            /d:sonar.login=""{_token}"" \
                            && dotnet restore \
                            && dotnet build -c Release \
                            && dotnet /sonar-scanner/SonarScanner.MSBuild.dll end \
                            /d:sonar.login=""{_token}""""";

            var processInfo = new ProcessStartInfo("docker", command)
            {
                CreateNoWindow = true,
                UseShellExecute = false,
                RedirectStandardOutput = true,
                RedirectStandardError = true
            };

            int exitCode;
            using (var process = new Process())
            {
                process.StartInfo = processInfo;
                process.OutputDataReceived  = ProcessOutputDataReceived;
                process.ErrorDataReceived  = ProcessErrorDataReceived;

                process.Start();
                process.BeginOutputReadLine();
                process.BeginErrorReadLine();
                process.WaitForExit(1200000);
                if (!process.HasExited)
                {
                    process.Kill();
                }

                exitCode = process.ExitCode;
                process.Close();
            }
        }

The code above fails with "docker: invalid reference format". Will be appreciated for any help

CodePudding user response:

The backslashes are there to tell the bash command line that more is coming. When running from C#, you don't need those.

With the way you've done it, your string also includes the CRLFs between each line. Those also have to go so you end up with a single long line with the command.

So something like this

var command = 
    "run -it --rm "  
    "-v ""/home/source3/mySolution:/myProj"" "  
    "nosinovacao/dotnet-sonar:latest "  
    "bash -c "  
    """cd myProj "  
    "&& dotnet /sonar-scanner/SonarScanner.MSBuild.dll begin "  
    $"/k:{projectKey} /name:{projectName} /version:1.0 "  
    $"/d:sonar.host.url=""{_baseUrl}"" "  
    $"/d:sonar.login=""{_token}"" "  
    "&& dotnet restore "  
    "&& dotnet build -c Release "  
    "&& dotnet /sonar-scanner/SonarScanner.MSBuild.dll end "  
    "/d:sonar.login=""{_token}""""";
  • Related