Closed EsZHONG closed 1 year ago
ContainerExecCreateResponse response = await dockerClient.Exec.ExecCreateContainerAsync(container, new ContainerExecCreateParameters()
{
AttachStderr = true,
AttachStdin = true,
AttachStdout = true,
Cmd = new List<string>() { "powershell.exe" },
Detach = false,
Tty = true
});
MultiplexedStream multiplexedStream = await dockerClient.Exec.StartAndAttachContainerExecAsync(response.ID, true);
var stdinTask = Task.Run(async () =>
{
await multiplexedStream.CopyFromAsync(Console.OpenStandardInput(), CancellationToken.None);
});
var stdoutTask = Task.Run(async () =>
{
await multiplexedStream.CopyOutputToAsync(
Console.OpenStandardInput(),
Console.OpenStandardOutput(),
Console.OpenStandardError(),
CancellationToken.None);
});
await Task.WhenAny(stdinTask, stdoutTask);
multiplexedStream.Dispose();
Got the answer from GPT, which seems to be working with a small format issue.
Hi,
I'm trying to start an interactive session against container like what
docker exec -i
would do. However, with the following code I'm only able to get the first batch of output (from powershell.exe) and seems whatever I enter after that was not executed in container.I've confirmed that inputs are wrote to the stream by setting a breakpoint at MultiplexedStream.WriteAsync(). But can't confirm if they're executed in container as no output can be seen and typing
exit
doesn't lead to anything.What version of Docker.DotNet?:
Can someone kindly share how to properly use the stream and start an interactive session? Or if it's simply not supported at all?
Thanks in advance!