Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docker outputs to standard out with added carriage return character #8513

Closed
itsafire opened this issue Oct 11, 2014 · 36 comments

Comments

@itsafire
Copy link
Contributor

commented Oct 11, 2014

Running docker run -ti --rm ubuntu:12.04 cat /etc/passwd >passwd.txt will result in a passwd file with added carriage return character at the end of each line. What is the rationale of this behavior ? Or is this to be considered a bug ?

My current Docker environment is:

docker@thor:~$ docker info
Containers: 4
Images: 179
Storage Driver: aufs
 Root Dir: /var/lib/docker/aufs
 Dirs: 187
Execution Driver: native-0.2
Kernel Version: 3.14.0-2-amd64
Operating System: Debian GNU/Linux jessie/sid
@estesp

This comment has been minimized.

Copy link
Contributor

commented Oct 13, 2014

Can you provide the output of docker version? I can't reproduce with ubuntu:latest or ubuntu:12.04, using a current docker master build (~1.3.0RC). If so, maybe this is an older bug that has been resolved.

@itsafire

This comment has been minimized.

Copy link
Contributor Author

commented Oct 14, 2014

$ docker --version
Docker version 1.2.0, build fa7b24f

@md5

This comment has been minimized.

Copy link
Contributor

commented Dec 3, 2014

I've been running into this issue as well. It was the cause of much grief that I reported at boot2docker/boot2docker#654

Here's a minimal test case for me:

$ docker run -t --rm debian echo -n $'\n' | od -c

When I run that in a Terminal window under OS X (connecting to a b2d DOCKER_HOST), I get this output:

0000000   \r  \n                                                        
0000002

Here's the same thing without -t:

$ docker run --rm debian echo -n $'\n' | od -c
0000000   \n                                                            
0000001
@md5

This comment has been minimized.

Copy link
Contributor

commented Dec 3, 2014

I should have added this:

$ docker version
Client version: 1.3.2
Client API version: 1.15
Go version (client): go1.3.3
Git commit (client): 39fa2fa
OS/Arch (client): darwin/amd64
Server version: 1.3.2
Server API version: 1.15
Go version (server): go1.3.3
Git commit (server): 39fa2fa
@jessfraz

This comment has been minimized.

Copy link
Contributor

commented Feb 26, 2015

I cannot reproduce on the latest version, please let me know if you can and I will reopen

@jessfraz jessfraz closed this Feb 26, 2015
@md5

This comment has been minimized.

Copy link
Contributor

commented Feb 26, 2015

@jfrazelle I just did exactly the same commands as #8513 (comment) and got the same results. I tried it both from my OS X terminal and from inside boot2docker ssh.

@jessfraz

This comment has been minimized.

Copy link
Contributor

commented Feb 26, 2015

ping @icecrime what was the stuff with hijack and cat that was fixed, @md5 i have reopened

@jessfraz jessfraz reopened this Feb 26, 2015
@jessfraz

This comment has been minimized.

Copy link
Contributor

commented Feb 26, 2015

I dont see what you are talking about tho

root@aa6c3c3e9164:/# cat /etc/nsswitch.conf 
# /etc/nsswitch.conf
#
# Example configuration of GNU Name Service Switch functionality.
# If you have the `glibc-doc-reference' and `info' packages installed, try:
# `info libc "Name Service Switch"' for information about this file.

passwd:         compat
group:          compat
shadow:         compat

hosts:          files dns
networks:       files

protocols:      db files
services:       db files
ethers:         db files
rpc:            db files

netgroup:       nis
root@aa6c3c3e9164:/# cat /etc/nsswitch.conf > test
root@aa6c3c3e9164:/# cat test
# /etc/nsswitch.conf
#
# Example configuration of GNU Name Service Switch functionality.
# If you have the `glibc-doc-reference' and `info' packages installed, try:
# `info libc "Name Service Switch"' for information about this file.

passwd:         compat
group:          compat
shadow:         compat

hosts:          files dns
networks:       files

protocols:      db files
services:       db files
ethers:         db files
rpc:            db files

netgroup:       nis
root@aa6c3c3e9164:/# 
@jessfraz

This comment has been minimized.

Copy link
Contributor

commented Feb 26, 2015

ohhhh i see its this

jessie at debian in ~/docker on skip-lxc-remaining-tests
$ docker run -ti --rm ubuntu:14.04 cat /etc/nsswitch.conf > test

jessie at debian in ~/docker on skip-lxc-remaining-tests [?]
$ cat test 
# /etc/nsswitch.conf
#
# Example configuration of GNU Name Service Switch functionality.
# If you have the `glibc-doc-reference' and `info' packages installed, try:
# `info libc "Name Service Switch"' for information about this file.

passwd:         compat
group:          compat
shadow:         compat

hosts:          files dns
networks:       files

protocols:      db files
services:       db files
ethers:         db files
rpc:            db files

netgroup:       nis

jessie at debian in ~/docker on skip-lxc-remaining-tests [?]

this is not something that can be fixed you see docker run performs a new line and the file itself was adding a new line so then you get two, does that make sense

@jessfraz jessfraz closed this Feb 26, 2015
@md5

This comment has been minimized.

Copy link
Contributor

commented Feb 26, 2015

@jfrazelle Did you try my command? The echo inside the container is only sending a single byte to stdout, i.e. \n. When it comes out of docker run with -t, it has an extra \r in front of the \n.

@md5

This comment has been minimized.

Copy link
Contributor

commented Feb 26, 2015

Client version: 1.5.0
Client API version: 1.17
Go version (client): go1.4.1
Git commit (client): a8a31ef
OS/Arch (client): darwin/amd64
Server version: 1.5.0
Server API version: 1.17
Go version (server): go1.4.1
Git commit (server): a8a31ef
@jessfraz

This comment has been minimized.

Copy link
Contributor

commented Feb 26, 2015

ya checkout my comment here #8513 (comment)

@mmb

This comment has been minimized.

Copy link

commented Apr 7, 2015

I'm also seeing these extra carriage returns in my output when docker run --tty. I'm on Linux so I should not be getting DOS line endings ("\r\n").

When I remove the --tty they aren't there.

user@host:/tmp# docker run --tty=true --interactive=true ruby:2.2.1 echo hi > out
user@host:/tmp# file out
out: ASCII text, with CRLF line terminators
user@host:/tmp# docker run --interactive=true ruby:2.2.1 echo hi > out
user@host:/tmp# file out
out: ASCII text
user@host:/tmp# docker version
Client version: 1.5.0
Client API version: 1.17
Go version (client): go1.4.1
Git commit (client): a8a31ef
OS/Arch (client): linux/amd64
Server version: 1.5.0
Server API version: 1.17
Go version (server): go1.4.1
Git commit (server): a8a31ef
@scr4bble

This comment has been minimized.

Copy link

commented Oct 5, 2015

@jfrazelle Did you try original reproducer #8513 (comment) ? I didn't get your explanation. Please can you provide some additional information about solution of this issue ? I would be very happy, if you can explain reason, why is this issue closed and not fixed - preferably on original reproducer. Thanks in advance :)

RedHat Bugzilla tracker:
https://bugzilla.redhat.com/show_bug.cgi?id=1260111

@pmbauer

This comment has been minimized.

Copy link

commented Oct 9, 2015

Ran into this issue today as well.
a docker run --tty=true ... outputs newlines with \r\n instead of \n
a docker run --tty=false ... outputs newlines with \n as expected

$ docker version && docker info
Client version: 1.6.2
Client API version: 1.18
Go version (client): go1.4.2
Git commit (client): 7c8fca2
OS/Arch (client): linux/amd64
Server version: 1.6.2
Server API version: 1.18
Go version (server): go1.4.2
Git commit (server): 7c8fca2
OS/Arch (server): linux/amd64
Containers: 18
Images: 255
Storage Driver: aufs
 Root Dir: /var/lib/docker/aufs
 Backing Filesystem: extfs
 Dirs: 291
 Dirperm1 Supported: true
Execution Driver: native-0.2
Kernel Version: 3.19.0-16-generic
Operating System: Ubuntu 15.04
CPUs: 2
Total Memory: 7.336 GiB
Name: 52.8.43.233
ID: S7PT:QYA4:VVEE:Z2ZJ:IWL2:GPNQ:QNZB:VTVA:GRUJ:EEBL:CVWH:5NFK
Username: pmbauer
Registry: [https://index.docker.io/v1/]
WARNING: No swap limit support
@pupssman

This comment has been minimized.

Copy link

commented Oct 22, 2015

Folks, what's up on that?
Still looks like a great deal of a bug to me.

@Globidev

This comment has been minimized.

Copy link

commented Nov 12, 2015

Still getting this weird behavior with docker 1.9

> docker run --rm busybox echo "Hello World" | cat -e
Hello World$

> docker run --rm -t busybox echo "Hello World" | cat -e
Hello World^M$
> docker version
Client:
 Version:      1.9.0
 API version:  1.21
 Go version:   go1.4.3
 Git commit:   76d6bc9
 Built:        Tue Nov  3 19:20:09 UTC 2015
 OS/Arch:      linux/amd64

Server:
 Version:      1.9.0
 API version:  1.21
 Go version:   go1.4.3
 Git commit:   76d6bc9
 Built:        Tue Nov  3 19:20:09 UTC 2015
 OS/Arch:      linux/amd64

> docker info
Containers: 18
Images: 176
Server Version: 1.9.0
Storage Driver: aufs
 Root Dir: /var/lib/docker/aufs
 Backing Filesystem: extfs
 Dirs: 212
 Dirperm1 Supported: true
Execution Driver: native-0.2
Logging Driver: json-file
Kernel Version: 3.19.0-33-generic
Operating System: Ubuntu 14.04.3 LTS
CPUs: 4
Total Memory: 7.781 GiB
Name: ubuntu
ID: CJNC:AI5U:HIYG:X4VN:IVWT:YSFE:HCTL:VQXH:EMP3:CMJN:UODG:CAYV
Username: globidocker
Registry: https://index.docker.io/v1/
WARNING: No swap limit support
@KholdStare

This comment has been minimized.

Copy link

commented Dec 2, 2015

I have the same issue still. Can this be reopened please?

@thaJeztah

This comment has been minimized.

Copy link
Member

commented Dec 3, 2015

@KholdStare reading back the discussion it looks like this couldn't be fixed in docker; #8513 (comment) but perhaps I've misunderstood @jfrazelle ?

@KholdStare

This comment has been minimized.

Copy link

commented Dec 3, 2015

@thaJeztah I'm sorry if I sound rude, but that referenced comment does not make any sense. @Globidev has an easily reproducible case where -t affects output, and it has been mentioned multiple times throughout the thread. Unfortunately, it does not look like @jfrazelle ran the suggested test case.

@pauldorn

This comment has been minimized.

Copy link

commented Jan 18, 2016

The misunderstanding appears to be that @jfrazelle has assumed you mean trailing linefeeds only.
It is affecting ALL linefeeds including embedded.

This is reproducible in 1.9.1 build a34a1d5

My use case is a dockerized script that produces a PDF.
When run inside the container for consumption in the container it works as expected.
When the container output is redirected to a file on the host with docker run -it --rm genpdf > broken.pdf it comes out with all embedded linefeeds converted to cr/lf.
This is clearly wrong.

@jessfraz jessfraz reopened this Jan 18, 2016
@cpuguy83

This comment has been minimized.

Copy link
Contributor

commented Jan 19, 2016

I am not seeing the extra carriage returns described.
Is there a simple repro other than docker run --rm busybox echo "Hello World" | cat -e?

Also, what platform is the docker client running on?

@ananace

This comment has been minimized.

Copy link

commented Jan 19, 2016

@cpuguy83
docker run -t --rm busybox echo "Hello World" | cat -e
It only happens when a tty is assigned.

This issue has been wrecking havoc for me on one of my newly dockerized personal projects, even managing to fit carriage returns into the log messages. Makes it rather impossible to access the tty for the project, as the terminals I have access to (PuTTY, MSYS) break when fed carriage returns.

@cpuguy83

This comment has been minimized.

Copy link
Contributor

commented Jan 19, 2016

Aha, thanks!

frankcarey added a commit to frankcarey/docker that referenced this issue Jan 26, 2016
…ker exec -it ..' commands.

Signed-off-by: Frank Carey <frank@devinci.io>
@frankcarey

This comment has been minimized.

Copy link

commented Jan 26, 2016

I started on a test for this in a PR, but got stuck dealing with getting it to work. #19700

However, I just figured out a workaround for bash scripts. If you set the command's stdin to /dev/null, then you can avoid the extra CR / \r / ^M / 0d

docker exec -it ... < /dev/null

@generalov

This comment has been minimized.

Copy link

commented Jan 27, 2016

Hm.. stdin redirection may be the some sort of disabling the terminal support.
As it is written above the docker doesn't append CR then terminal doen't enabled.

@caioalvs

This comment has been minimized.

Copy link

commented Apr 8, 2016

Folks,

I managed to workaround this issue by pipeline output to sed -e "s/\r//" to replace the carriage returns.

Using the official hello-world image for example, if output is redirected to a file the content appears with the ^Mcharacter.

docker run --rm --tty hello-world > /tmp/hello-world.txt

Applying sed -e "s/\r//" using a pipeline, the file content is clear of carriage returns.

docker run --rm --tty hello-world | sed -e "s/\r//" > /tmp/hello-world.txt

@thaJeztah

This comment has been minimized.

Copy link
Member

commented May 2, 2016

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

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

So, not sure what should be done here; the default line-endings used by TTY is CRLF, and that's what is visible here

@md5 @cpuguy83 wdyt?

@cpuguy83

This comment has been minimized.

Copy link
Contributor

commented May 2, 2016

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

@thaJeztah

This comment has been minimized.

Copy link
Member

commented May 6, 2016

Given #8513 (comment), I agree, this is not something in Docker, but the way TTY's work. I'll close this issue.

@thaJeztah thaJeztah closed this May 6, 2016
@scr4bble

This comment has been minimized.

Copy link

commented May 9, 2016

@thaJeztah Thanks for nice explanation.

@Globidev

This comment has been minimized.

Copy link

commented May 10, 2016

I tried to understand how default TTY options were set but Google didn't help me very much
If anyone is knowledgeable on the subject I would love to hear an explanation

Also, if anyone is able to explain to me why on a gnome terminal (default one on Ubuntu distros) and with a recent bash, when you run stty -a you see that the onlcr flag is set yet the terminal doesn't show carriage returns.

I made a simple version of stty to display stdout's terminal attributes
If you compile it statically, you can make an image from scratch only containing the resulting binary and run a container from it to see the default TTY attributes (I didn't want to run say stty on a busybox because I wasn't sure if the filesystem had an impact or not on the default TTY options).

code here
You can even compile it with docker:

docker run --rm -v $PWD:/app -w /app gcc:5.3 gcc -static -o tiny-stty main.c

Make a image from scratch:

FROM scratch

ADD tiny-stty /
ENTRYPOINT ["/tiny-stty"]

build and run it:

docker build -t tiny-stty .
docker run --rm -t tiny-stty
@Globidev

This comment has been minimized.

Copy link

commented May 10, 2016

Just found out about <sys/ttydefaults.h>
Default TTY options seem to be set by the kernel (which makes my little experiment above useless)

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 is indeed there

So yeah, I guess if you wanted to change the default TTY attributes of your containers run with -t transparently (without using @thaJeztah method), you would have to use a docker daemon on a linux compiled with different values for the flags above.
Seems a bit far fetched though. Time to go to bed now :)

leandro-lucarella-sociomantic added a commit to leandro-lucarella-sociomantic/beaver that referenced this issue Aug 9, 2017
Even when running docker manually and interactively one usually pass -ti,
for running tests this is not usually desired, specially if the output of
docker run needs to be captured (see
moby/moby#8513 for more details on this issue).
mihails-strasuns-sociomantic added a commit to sociomantic-tsunami/beaver that referenced this issue Aug 16, 2017
Even when running docker manually and interactively one usually pass -ti,
for running tests this is not usually desired, specially if the output of
docker run needs to be captured (see
moby/moby#8513 for more details on this issue).
@dragon788

This comment has been minimized.

Copy link
Contributor

commented Oct 14, 2017

Just ran into this when using tab completion on a command that runs from within a container (a la @jessfraz 's awesome .dockerfunc stuff). I am using @tianon 's github-hub, and I couldn't figure out when trying to hub add somefile<Tab> it was appending ^M after the filename. Looking here I was able to drop the -t from my docker run -i <lotsOstuff> and it still functions for hub push et al, so I'm happy, but confused why this behavior can't seem to be overridden.

samrocketman added a commit to samrocketman/jenkins-bootstrap-shared that referenced this issue Jun 29, 2018
Using the TTY means that the password included a carriage return.  This
was because of the `-t` option and not specific to docker.

See also:

- moby/moby#37366
- moby/moby#8513 (comment)
calaldees added a commit to superLimitBreak/mediaTimelineRenderer that referenced this issue Jul 8, 2018
docker was adding extra \r all over the place
moby/moby#8513
Fixed it by removing '-it' in docker command ... what a ballache
slintes added a commit to slintes/kubevirt that referenced this issue Sep 17, 2018
virtctl vnc didn't work with CRI-O, because the remote exec command in
virt-api converted LFs into CR + LF, which broke the VNC network protocol.
The root cause for this is that this seems to be a default behaviour
of tty, see moby/moby#8513 (comment).
The solution is to not use tty, we don't need it.
(Still unclear why this only happened with CRI-O)

Signed-off-by: Marc Sluiter <marc@slintes.com>
slintes added a commit to slintes/kubevirt that referenced this issue Sep 17, 2018
virtctl vnc didn't work with CRI-O, because the remote exec command in
virt-api converted LFs into CR + LF, which broke the VNC network protocol.
The root cause for this is that this seems to be a default behaviour
of tty, see moby/moby#8513 (comment).
The solution is to not use tty, we don't need it.
(Still unclear why this only happened with CRI-O)

Signed-off-by: Marc Sluiter <msluiter@redhat.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
You can’t perform that action at this time.