为什么“ docker run -t”输出在命令输出中包含\r? [英] Why do "docker run -t" outputs include \r in the command output?

查看:233
本文介绍了为什么“ docker run -t”输出在命令输出中包含\r?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Docker客户端版本:18.09.2。

I'm using Docker client Version: 18.09.2.

以交互方式启动容器并运行日期命令,然后将其输出通过管道传送到 hexdump 进行检查,我看到尾随的 \n 按预期:

When I run start a container interactively and run a date command, then pipe its output to hexdump for inspection, I'm seeing a trailing \n as expected:

$ docker run --rm -i -t alpine
/ # date | hexdump -c
0000000   T   h   u       M   a   r           7       0   0   :   1   5
0000010   :   0   6       U   T   C       2   0   1   9  \n
000001d

但是,当我直接通过 date 命令作为入口点并运行容器时,我得到了每次输出中有新行时,都会 $ \r \n

However, when I pass the date command as an entrypoint directly and run the container, I get a \r \n every time there's a new line in the output.

$ docker run --rm -i -t --entrypoint=date alpine | hexdump -c
0000000   T   h   u       M   a   r           7       0   0   :   1   6
0000010   :   1   9       U   T   C       2   0   1   9  \r  \n
000001e

这很奇怪。

当我忽略 -t (不分配任何TTY):

It totally doesn't happen when I omit -t (not allocating any TTY):

docker run --rm -i --entrypoint=date alpine | hexdump -c
0000000   T   h   u       M   a   r           7       0   0   :   1   7
0000010   :   3   0       U   T   C       2   0   1   9  \n
000001d

这是怎么回事?

这听起来很危险,因为我使用在脚本中使用docker run 命令,如果我忘记从脚本中省略 -t ,我将从<$ c $收集的输出c> docker run 命令将具有不可见 /不可打印的 \r 字符,这可能会导致各种问题。

This sounds dangerous, as I use docker run command in my scripts, and if I forget to omit -t from my scripts, the output I'll collect from docker run command will have invisible/non-printible \r characters which can cause all sorts of issues.

推荐答案

tldr;这是 tty 的默认行为,与Docker无关。根据在github上提交的有关您的确切问题的票证

tldr; This is a tty default behaviour and unrelated to docker. Per the ticket filed on github about your exact issue.

在该票证中引用相关注释:

Quoting the relevant comments in that ticket:


默认情况下,确实好像TTY会将换行符转换为CRLF

Looks like this is indeed TTY by default translates newlines to CRLF



$ docker run -t --rm debian sh -c "echo -n '\n'" | od -c
0000000   \r  \n
0000002




禁用stty -onlcr正确地将将换行符转换为回车换行符;

disabling "translate newline to carriage return-newline" with stty -onlcr correctly gives;



$ docker run -t --rm debian sh -c "stty -onlcr && echo -n '\n'" | od -c
0000000   \n
0000001




默认的TTY选项似乎是由内核设置的...在我的Linux主机上,它包含:

Default TTY options seem to be set by the kernel ... On my linux host it contains:



/*
 * Defaults on "first" open.
 */
#define TTYDEF_IFLAG    (BRKINT | ISTRIP | ICRNL | IMAXBEL | IXON | IXANY)
#define TTYDEF_OFLAG    (OPOST | ONLCR | XTABS)
#define TTYDEF_LFLAG    (ECHO | ICANON | ISIG | IEXTEN | ECHOE|ECHOKE|ECHOCTL)
#define TTYDEF_CFLAG    (CREAD | CS7 | PARENB | HUPCL)
#define TTYDEF_SPEED    (B9600)




ONLCR 确实在那里。

当我们查看 ONLCR 标志文档,我们可以看到:

When we go looking at the ONLCR flag documentation, we can see that:


[-] onlcr :将换行符转换为回车换行符

[-]onlcr: translate newline to carriage return-newline

再次引用 github票证


故事的寓意,除非要TTY,否则不要使用-t。

TTY行的结尾是CRLF,这不是D ocker的行为。

Moral of the story, don't use -t unless you want a TTY.
TTY line endings are CRLF, this is not Docker's doing.

这篇关于为什么“ docker run -t”输出在命令输出中包含\r?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆