How to call docker run from c# application

2020-08-25 05:17发布

I've got a WPF application that whilst processing a file needs to use a docker process. The docker container is built on the box, currently after processing a file with the WPF application the user has to start a command prompt and type in

docker run --it --rm -v folderdedirect process parameters_including_filePath

to do further processing.

I want to include that in the WPF application. I could presumably use system.diagnostics.process with cmd.exe? I looked at the Docker.dotnet but couldn't for the life of me work out how it's supposed to just run a local container.

标签: c# wpf docker
2条回答
贪生不怕死
2楼-- · 2020-08-25 05:52

Here's how I did it in the end but there may be a better way.

var processInfo = new ProcessStartInfo("docker", $"run -it --rm blahblahblah");

processInfo.CreateNoWindow = true;
processInfo.UseShellExecute = false;
processInfo.RedirectStandardOutput = true;
processInfo.RedirectStandardError = true;

int exitCode;
using (var process = new Process())
{
    process.StartInfo = processInfo;
    process.OutputDataReceived += new DataReceivedEventHandler(logOrWhatever());
    process.ErrorDataReceived += new DataReceivedEventHandler(logOrWhatever());

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

    exitCode = process.ExitCode;
    process.Close();
}
查看更多
混吃等死
3楼-- · 2020-08-25 05:52

Adapted the code above for my context. The hardest part is determining how you want to monitor if your process finished. You can try setting up event listeners as shown here. Having other conditions to verify (if client sends a kill signal), I decided to just continually monitor process.HasExited value.

private static void RunDockerImage(ContainerData containerData)
    {
        var processInfo = new ProcessStartInfo("docker", $"run "+containerData.ImageName);

        processInfo.CreateNoWindow = true;
        processInfo.UseShellExecute = false;
        processInfo.RedirectStandardOutput = true;
        processInfo.RedirectStandardError = true;

        int exitCode;
        using (var process = new Process())
        {
            process.StartInfo = processInfo;

            // indicate process is started
            StartUpdateOrch(containerData);

            process.Start();
            process.BeginOutputReadLine();
            process.BeginErrorReadLine();

            while(!process.HasExited)
            {
                // continually check if kill signal is set
                if (getKillSignal())
                {
                    process.Kill();
                } 
                Thread.Sleep(2000);
            }

            exitCode = process.ExitCode;
            containerData.exitCode = exitCode;

            // indicate process is done
            FinishUpdateOrch(containerData);

            process.Close();
        }
    }
查看更多
登录 后发表回答