Separare stdout e stderr per `docker run`?

Oct 27 2020

Posso ottenere pipe stdout e stderr separate per docker run?

Esempio:

$ 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

Cosa mi aspetterei:

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

Risposte

2 rosaroteelfe Oct 27 2020 at 19:42

Il problema per te è l'opzione "-t". Dopo averlo rimosso, stderr dovrebbe funzionare come previsto:

$ 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 usi l'opzione '-t' (o --tty), stdout e stderr vengono uniti insieme. Questo non è documentato in modo esplicito nella finestra mobile perché "Questo è il comportamento normale di una tty". [Fonte: numero di moby 25542, commento 238647584 di bamarni ]