Separar stdout e stderr para `docker run`?

Oct 27 2020

Posso obter tubos stdout e stderr separados para docker run?

Exemplo:

$ docker run --rm -it alpine sh -c 'echo this is stdout; echo this is stderr >&2' \ 2> stderr.txt this is stdout this is stderr $ cat stderr.txt

O que eu esperaria:

$ sh -c 'echo this is stdout; echo this is stderr >&2' 2> stderr.txt this is stdout $ cat stderr.txt
this is stderr

Respostas

2 rosaroteelfe Oct 27 2020 at 19:42

O problema para você é a opção '-t'. Depois de removê-lo, o stderr deve funcionar conforme o esperado:

$ docker run -i --rm alpine sh -c 'echo this is stdout; echo this is stderr >&2' 2>stderr this is stdout $ cat stderr
this is stderr

Quando você usa a opção '-t' (ou --tty), stdout e stderr são unidos. Isso não está explicitamente documentado no docker porque "Esse é o comportamento normal de um tty." [Fonte: moby issue 25542, comentário 238647584 de bamarni ]