console-rs / indicatif

A command line progress reporting library for Rust
MIT License
4.45k stars 243 forks source link

No progress bar in docker logs #651

Open funlennysub opened 4 months ago

funlennysub commented 4 months ago

When you run an app that has a progress bar it won't be rendered when attaching to container logs, but will be present if ran via cargo run

Sample repo: https://github.com/funlennysub/fuzzy-octo-pancake

Steps to reproduce:

  1. cargo run -r - progress bar will be displayed
  2. docker build --tag 'whatever' .
  3. docker run 'whatever' - progress bar won't be displayed
djc commented 4 months ago

I think this is more or less intentional -- indicatif checks whether the stream it is attached to is a terminal, which is probably not the case in this scenario. I think #530 covers this scenario.

funlennysub commented 4 months ago

Oop, sorry for not replying for a while. Could you explain a bit more why this happens when output is being written to a file, so i can search for a solution?

chris-laplante commented 4 months ago

Oop, sorry for not replying for a while. Could you explain a bit more why this happens when output is being written to a file, so i can search for a solution?

It is intentional, as @djc mentioned. The underlying reason is that it is hard to say what should actually be written to the file. Should we write the progress bar just once (when it is 'complete'), or each time the progress ticks? What about every time the status text changes? etc etc etc.

A workaround, as mentioned in @530, is to use the 'unbuffer' tool: https://linux.die.net/man/1/unbuffer.

funlennysub commented 4 months ago

Im not sure how unbuffer suppose to work in my case, i tried using these lines in my Dockerfile:

chris-laplante commented 3 months ago

I'm still not quite sure what you're trying to do, but perhaps this will help: https://github.com/chris-laplante/fuzzy-octo-pancake/commit/6cb2eb1226d43ff5ef60872496e888f1ac11df79.

It uses unbuffer to capture the output to a file called out.txt, then prints the contents of that file after the program is done. When I run it, I get:

chris@chris-virtual-machine:/tmp/fuzzy-octo-pancake$ docker run --rm -it 702330d842de2bc4236cc9e128c6f19a8df07908c2cfcc3131fb0e48b0be37c3
Progress bar
[00:00:05] ######################################## 500/500 Done                                                                                                                                                                       Hello, world!
funlennysub commented 3 months ago

then prints the contents of that file after the program is done

Sadly i need real-time progress bar updates, or at least close to it

chris-laplante commented 3 months ago

then prints the contents of that file after the program is done

Sadly i need real-time progress bar updates, or at least close to it

Real-time but where? Printed to a file, or output to the console?

funlennysub commented 3 months ago

then prints the contents of that file after the program is done

Sadly i need real-time progress bar updates, or at least close to it

Real-time but where? Printed to a file, or output to the console?

in $ docker logs ...

djc commented 3 months ago

I'd prefer to keep this discussion in #530 so we have all of it in one place. I think it would make sense to expose some API to override the implicit terminal detection logic.