惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园 - Franky
J
Java Code Geeks
腾讯CDC
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Jina AI
Jina AI
博客园 - 司徒正美
Stack Overflow Blog
Stack Overflow Blog
美团技术团队
L
LangChain Blog
WordPress大学
WordPress大学
A
About on SuperTechFans
Martin Fowler
Martin Fowler
月光博客
月光博客
Y
Y Combinator Blog
U
Unit 42
D
Docker
Recent Announcements
Recent Announcements
Hugging Face - Blog
Hugging Face - Blog
B
Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
G
Google Developers Blog
Last Week in AI
Last Week in AI
T
The Blog of Author Tim Ferriss
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com

kkocdko's blog

Tasker 永久试用修改思路 - kkocdko's blog 极限压缩文档扫描件 - kkocdko's blog Bypass the Blank Chars Detection crknob - Chrome portable patch 我如何将博客首页体积降至 3 KB - kkocdko's blog 如何极限优化压缩文件体积 - kkocdko's blog EmEditor 精简版 - kkocdko's blog Backup Data from VSCode Web 优雅地游玩 Minecraft - kkocdko's blog 让程序以管理员权限运行的 PE 清单 - kkocdko's blog 注销 ZEIT 账号之后遇到的坑 - kkocdko's blog 本博客的协议已更改为 CC0 - kkocdko's blog mobp - 使 MSOffice 后台常驻 在安装 VS 生成工具时规避 .NET 安装错误 各种格式的占位空文件 - kkocdko's blog 优雅地游玩 Flash 游戏 - kkocdko's blog WPS 2016 极限精简版 - kkocdko's blog Convert Const to Let by Terser 规避 Visual Studio 许可证过期的 Bug 方便地使用 iPod - kkocdko's blog 用 UserJS 注入 UserCSS - kkocdko's blog 注册表修改文件关联(默认程序) - kkocdko's blog 周期精准的太阳系模型 - kkocdko's blog 关闭屏幕的 WinAPI - kkocdko's blog 重置 Windows 图标缓存 - kkocdko's blog AIDA64 极限精简单文件版 - kkocdko's blog 几何画板单文件精简版 - kkocdko's blog 世界之窗浏览器单文件版 - kkocdko's blog 在 MBR + Legacy 下用 Bootmgr 引导 Ubuntu 简洁的 Markdown 预览工具 - kkocdko's blog
Fix Docker 'input device is not a TTY' elegantly
2026-04-11 · via kkocdko's blog

TLDR

docker exec -it kblog df -> script -c 'docker exec -it kblog df' /dev/null

Explanation

Let's look at a common scenario. We try to create a container using an Ubuntu image and execute something inside.

# Run the container in the background
docker run -d --name kblog ubuntu tail -f /dev/null
# Execute the command inside the container (`-i -t` is shortened to `-it`)
docker exec -it kblog df

But if you write commands above to file a.sh and run a.sh >a.txt, you will see ... input device is not a TTY ... in a.txt.

According to docs, the -t switch will alloc a pseudo-TTY. What does this mean? Let's start with the phenomenon: without -i, the read command inside the container can't read user input from stdin; without -t, you can't use the Ctrl + C to send SIGINT / SIGTERM to exit the program.

So we often use -it to make running a program inside a container work like running it natively (blocking, able to type in, able to use the Ctrl + some key). But since the docker command is not in a pty environment when using redirect / as a child process, the -t option then causes error.

The most popular solution is removing -t switch, or use some auto-detection to remove, which require huge modifications in existing shell script, bringing a lot of inconsistencies.

I think a better way would be to:

Use the script command, it provides the ability to run commands innner pty. Just script -c 'a.sh' a.txt, then docker exec -it kblog df in a.sh will no longer report errors. If you don't need to save the output, just change the last argument to /dev/null.

TTY in Linux is a long-established thing. The serial port or ttyUSB is the closest to the original usage. After we open Terminal, we use pseudo-TTY or PTY for short, and the mechanism is more complex, so read the documentation if you are interested.

Here is a special point: many people think that puts("\n") will sync stdout's buffer, which is actually wrong. The "refresh on newline encounter" is provided by PTY, stdout is just fd 1 and has no such special feature. If you look at stdout in real time, using a hook or some other methods, you will be able to see this.

But you may find that in some languages, the print statement does immediately sync stdout on newline, such as Golang's fmt.Println("hi"). This is an operation that the language or language std lib does on its own. We should not rely on this language or library specific behavior if not necessary.

Reference: