Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call docker run from c# application

Tags:

c#

docker

wpf

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.

like image 947
dibs487 Avatar asked Jul 25 '17 16:07

dibs487


1 Answers

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();
}
like image 190
dibs487 Avatar answered Sep 18 '22 17:09

dibs487