jq
jq copied to clipboard
docker run -it | jq - breaks jq formatting
Describe the bug
When json is output from a docker run -it
command into jq, the output of the json is broken. there are extra spaces inserted causing a stair stepping of the resulting output.
docker run -i
and docker run -t
do not exhibit this behavior.
To Reproduce execute the following commands in your bash shell
bash -c 'for i in {1..5}; do echo "{\"a\":$i}"; done' | jq -c '.'$line'
docker run bash -c 'for i in {1..5}; do echo "{\"a\":$i}"; done' | jq -c '.'
docker run -i bash -c 'for i in {1..5}; do echo "{\"a\":$i}"; done' | jq -c '.'
docker run -t bash -c 'for i in {1..5}; do echo "{\"a\":$i}"; done' | jq -c '.'
docker run -it bash -c 'for i in {1..5}; do echo "{\"a\":$i}"; done' | jq -c '.'
Expected behavior output is correctly formatted.
Environment (please complete the following information):
❯ sw_vers
ProductName: Mac OS X
ProductVersion: 10.15.3
BuildVersion: 19D76
❯ docker --version
Docker version 19.03.5, build 633a0ea
❯ jq --version
jq-1.6
Additional context
perhaps i'm not understanding docker run -it
(wtf is a TTY)?
-i, --interactive Keep STDIN open even if not attached
-t, --tty Allocate a pseudo-TTY
maybe a bug in docker?
related to: https://github.com/stedolan/jq/issues/1414
It's natural that we can't expect correct behavior with piping against tty output. Don't use -t
flag of docker when you use pipe from its output. This behavior is not a bug of docker nor of jq. The sponge
from moreutils
seems to solve the problem but I'm rather surprised that we can't see the tty output as well (like echo '{"a":1}\n{"b":2}\n' >/dev/tty | jq .
).
I'm rather surprised that we can't see the tty output as well (like echo '{"a":1}\n{"b":2}\n' >/dev/tty | jq .).
Hi @itchyny ,I execute echo '{"a":1}\n{"b":2}\n' >/dev/tty | jq .
on Linux and Windows , both platform output {"a":1}\n{"b":2}\n
.
in reply to @itchyny:
stdbuf
from coreutils
also seems to work, no need to install sponge
from moreutils
as in:
docker run -it bash -c 'for i in {1..5}; do echo "{\"a\":$i}"; done' | stdbuf -o 10M jq '.'
(and for people finding this thread via search engine: yes, docker compose exec
leads to this issue as well)