如何从C#应用程序调用docker run [英] How to call docker run from c# application

查看:501
本文介绍了如何从C#应用程序调用docker run的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个WPF应用程序,该文件在处理文件时需要使用docker进程。 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

进行进一步处理。

我想将其包含在WPF应用程序中。我大概可以将 system.diagnostics.process cmd.exe 一起使用吗?我看了 Docker.dotnet ,但我一生都无法弄清楚它应该如何运行本地容器。

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.

推荐答案

这是我最后做的方式,但是可能有更好的方法。

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

这篇关于如何从C#应用程序调用docker run的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆