gpt4 book ai didi

docker-compose 不会将标准输入附加到 .net core 应用程序 Console.ReadLine

转载 作者:行者123 更新时间:2023-12-02 08:18:29 24 4
gpt4 key购买 nike

我在使用 docker-compose 时注意到奇怪的行为。 Console.ReadLine()不等待用户输入。

观察以下简化的 dot net core c# 控制台应用程序:

private static void Main(string[] args)
{
Console.WriteLine("Input something:");
while (true)
{
var line = Console.ReadLine();
Console.WriteLine($"{DateTime.Now} Input was: {line}");
System.Threading.Thread.Sleep(1000);
}
}

如果您通过 docker run -i readline 运行此命令工作正常:

enter image description here

但是如果你使用docker-compose up它只是循环而不等待 STDIN 上的输入:

enter image description here

这是为什么?

您可以使用以下方法自行尝试:

git clone https://github.com/mh-qUjB/docker-dotnetcore-readline.git
cd ./docker-dotnetcore-readline/ReadLine
docker-compose up

我目前正在测试:

  • Ubuntu 16.04
  • Docker version 18.06.1-ce, build e68fc7a
  • docker-compose version 1.22.0, build f46880fe
  • <TargetFramework>netcoreapp2.1</TargetFramework>
  • Dockerfile 请参阅 here

谢谢。

PS:如果您可以改进这个问题的标题,我会很高兴。

最佳答案

您可以找到单个容器并在该容器中执行命令。 Docker compose 正在流式传输您的日志,因此它不为您提供交互式 shell。

举个例子,假设您有这个撰写文件,

version: '3'

services:
nats:
image: nats-streaming
mongo:
image: mongo

您可以在每个容器中执行命令,如下所示

docker-compose exec mongo sh
docker-compose exec nats sh

关于docker-compose 不会将标准输入附加到 .net core 应用程序 Console.ReadLine,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52909821/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com