首页 > 解决方案 > 在 Windows 上将标准输入管道传输到 docker exec

问题描述

我正在尝试将 SQL 数据导入 docker 容器。使用 git bash / mintty:

> docker exec -it 79c5c16acca0 mysql -uusername -ppassword dbname
the input device is not a TTY.  If you are using mintty, try prefixing the command with 'winpty'

> winpty docker exec -it 79c5c16acca0 mysql -uusername -ppassword dbname
stdin is not a tty

我也尝试过 Powershell:

> Get-Content powo.sql | docker exec -it 79c5c16acca0 mysql -uusername -ppassword dbname
the input device is not a TTY.  If you are using mintty, try prefixing the command with 'winpty'

FWIW,bash在容器中运行正常:

> docker exec -it 79c5c16acca0 bash
root@79c5c16acca0:/#

标签: dockerexecstdintty

解决方案


如果您删除-t.

$ docker run --rm --name example alpine sleep 100

$ docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS          PORTS                 NAMES
49968909e3e4   alpine    "sleep 100"              8 seconds ago    Up 7 seconds                          example

$ echo "hi" | docker exec -it example cat
the input device is not a TTY.  If you are using mintty, try prefixing the command with 'winpty'

$ echo "hi" | docker exec -i example cat
hi

这适用于bashps和 Git bash。

这个答案是我能找到的关于做什么-i-t做什么的最佳参考(用我能理解的语言)。

-t据此,我的猜测是,当管道输入内容时,您不能使用,因为-t这意味着输入来自终端设备,但在这种情况下,输入是管道(它本身来自终端设备)。


推荐阅读