jq icon indicating copy to clipboard operation
jq copied to clipboard

docker run -it | jq - breaks jq formatting

Open jasonchester opened this issue 5 years ago • 3 comments

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 '.'

image

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

jasonchester avatar Feb 19 '20 22:02 jasonchester

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 .).

itchyny avatar Feb 20 '20 02:02 itchyny

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.

Alanscut avatar Feb 28 '20 07:02 Alanscut

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)

d56470 avatar Feb 18 '22 20:02 d56470