当我从 C# 应用程序执行 docker 命令时,出现 "docker:invalid reference format" 错误

When I execute docker command from C# app I get "docker:invalid reference format" error

我已经阅读了所有类似的问题,但没有找到答案。 我有 docker-命令,它在 Linux 命令布什 (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>""

它是 Sonarcube 扫描仪,它分析位于 linux 目录 /home/source3/mySolution 中的项目。 现在我尝试通过 C# 控制台应用程序执行相同的命令。我尝试通过Visual Studio和Docker申请运行,结果是一样的。我的代码是:

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();
            }
        }

上面的代码失败并显示“docker:引用格式无效”。如有任何帮助,我们将不胜感激

那里的反斜杠告诉 bash 命令行还有更多内容即将到来。当 运行 来自 C# 时,您不需要这些。

按照您的操作方式,您的字符串还包括每行之间的 CRLF。那些也必须去,所以你最终得到一个长行的命令。

像这样

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}""""";