Writing to Stdin using WriteAsync on attached container waits forever?
- Dominant language
- C#
- Stars
- 2.4k
- Forks
- 416
- PR merge metrics
- No merged PRs in 30d
Description
I'm trying trying to `run` command with some input from `Stdin` and process it in my container.
But when I the following command the process seems to "hang" and wait for something to happen.
```c#
var buffer = System.IO.File.ReadAllBytes("input.jpg");
await stream.WriteAsync(buffer.Concat(Encoding.ASCII.GetBytes(Environment.NewLine)).ToArray(), 0, buffer.Length, default(CancellationToken));
```
The container seems to start but it does not proceed with the execution of the command?
```
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1c17e536d0a2 a7c4412f004e "smartcrop --width..." 1 second ago Up Less than a second dreamy_bhaskara
```
I can't figure out what I am missing? [This issue](https://github.com/Microsoft/Docker.DotNet/issues/197#issuecomment-293769691) seems to be resolved by adding a new line `\n` at the end of the buffer but that doesn't work for me?
**Output of `dotnet --info`:**
```
.NET Command Line Tools (1.0.4)
Product Information:
Version: 1.0.4
Commit SHA-1 hash: af1e6684fd
Runtime Environment:
OS Name: ubuntu
OS Version: 16.04
OS Platform: Linux
RID: ubuntu.16.04-x64
Base Path: /usr/share/dotnet/sdk/1.0.4
```
**What version of Docker.DotNet?:**
```
2.124.3
```
**Steps to reproduce the issue:**
1. Copy paste the following Dockerfile
```
FROM ficusio/node-alpine:latest
RUN apk --no-cache add \
imagemagick \
ghostscript \
libjpeg \
jpeg-dev \
zlib \
zlib-dev \
--update curl
RUN npm install -g smartcrop-cli
CMD [ "echo", "Use one of the following commands [ animate | compare | composite | conjure | convert | display | identify | import | mogrify | montage | stream ]" ]
```
2. Copy paste the following code
```c#
class Program
{
static void Main(string[] args)
{
Task.Run(() => MainAsync()).GetAwaiter().GetResult();
}
static async Task MainAsync()
{
bool? isTTY = false;
var token = default(CancellationToken);
ContainerAttachParameters containerAttachParameters = null;
CreateContainerResponse createContainerResponse = null;
MultiplexedStream stream = null;
Task streamTask = null;
Stream stdOutputStream = new MemoryStream();
var client = new DockerClientConfiguration(new Uri("unix:///var/run/docker.sock")).CreateClient();
Config createContainerParameters = new Config {
Image = "a7c4412f004e",
ArgsEscaped = false,
AttachStderr = false,
AttachStdin = true,
AttachStdout = true,
Cmd = new string[] {
"smartcrop",
"--width", "100",
"--height", "100",
"-", "-"
},
OpenStdin = true,
StdinOnce = true,
};
try
{
createContainerResponse = await client.Containers.CreateContainerAsync(new CreateContainerParameters(createContainerParameters));
containerAttachParameters = new ContainerAttachParameters
{
Stream = true,
Stderr = false,
Stdin = true,
Stdout = true,
};
}
catch (Exception exception)
{
Console.Write(exception.Message);
}
try
{
if (containerAttachParameters != null)
{
stream = await client.Containers.AttachContainerAsync(createContainerResponse.ID, isTTY.GetValueOrDefault(), containerAttachParameters, token);
var buffer = System.IO.File.ReadAllBytes("input.jpg");
await stream.WriteAsync(buffer.Concat(Encoding.ASCII.GetBytes(Environment.NewLine)).ToArray(), 0, buffer.Length, default(CancellationToken));
streamTask = stream.CopyOutputToAsync(null, stdOutputStream, null, default(CancellationToken));
}
if (!await client.Containers.StartContainerAsync(createContainerResponse.ID, new ContainerStartParameters()))
{
throw new Exception("The container has already started.");
}
if (containerAttachParameters != null)
{
await streamTask;
stdOutputStream.Position = 0;
using(var filestream = System.IO.File.Create($"./out/{DateTime.Now.Second}.jpg")) {
await stdOutputStream.CopyToAsync(filestream);
}
}
}
catch(Exception ex) {
Console.Write(ex.Message);
}
finally
{
stream?.Dispose();
}
}
}
```
3. Run `dotnet run`
**What actually happened?:**
The process keeps hanging, like it waits for input or something?
**What did you expect to happen?:**
Input to be written and processed by the container.
**Additional information:**
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.