gabrieldemarmiesse / python-on-whales

An awesome Python wrapper for an awesome Docker CLI!
MIT License
514 stars 100 forks source link

Is there a way to prevent any output from docker-compose up, build etc? #292

Open lodif opened 2 years ago

lodif commented 2 years ago

As the title suggests, I would like to prevent any output from python-on-whales. Is there a way to do it?

Thanks!

gabrieldemarmiesse commented 2 years ago

Currently it's only available on some commands. For exemple docker.buildx.build. I'll work on improving this area :)

gabrieldemarmiesse commented 2 years ago

Just for my personal curiosity and also to better satisfy the request, could you tell me what's your use case? What you use python-on-whales for and why you want to supress the output?

lodif commented 2 years ago

Sorry for the delay. It was for a univeristy project where I had to trigger many times different docker-compose files. The CLI had a custom progress bar to show the status of the application and for this reason was better to prevent any other log. Anyway we were able to use subprocess to do that. Feel free to close this issue and thanks for your great work!

gabrieldemarmiesse commented 2 years ago

Thanks! I believe your use case is valid and should be in the scope of python-on-whales. I'll see what's the best way to implement it, so that the interface is intuitive!

sj-lt commented 1 year ago

Hi, Dunno if should open another issue but problem is related somehow. For my use case there is on contrary no enough output for docker compose commands. For example when i use docker.compose.up() or pull() there is no output at all while building or downloading images. Got some digging and it looks like docker compose is directing this output to stderr related compose issue . I am using your library for some startup script and like it a lot. But at first run is little sad to see nothing while 10 images are being pulled, looks kinda stuck. Maybe there should be some flag to prevent piping/capturing std/stderr or some more control over it. Cheers.

gabrieldemarmiesse commented 1 year ago

Thank you, this is great feedback, I'll look into it :)

gabrieldemarmiesse commented 1 year ago

After this last PR is merged, docker.compose up, build and pull will all have a quiet flag, off by default, and I ensured everything was displayed on the screen when the flag is off. With this I think we can close this issue :)

sj-lt commented 1 year ago

Thanks for implementation that works great now for starting but there is still no output when using down command. Forgot to mention it in previous comment ;/

gabrieldemarmiesse commented 1 year ago

Thanks for the report! I'll fix it

baptistecolle commented 8 months ago

It seems that it is not possible to capture the stdout of the methods. Indeed, the stdout or stderr is not inherited from the main thread. Is there a way to capture the output of the commands?

Code:

sys.stdout = open(os.devnull, "w")
sys.stderr = open(os.devnull, "w")

print("This will not be printed")

self.docker_old.login(registry=server, username=username, password=password)

sys.stdout = old_stdout
sys.stderr = old_stderr

print("This will be printed")

Expected Behaviour:

This will be printed

Actual Behaviour

Login Succeeded
This will be printed

The only solution to this issue that I found is this, but this seems more like a hack. Indeed, exceptions that are raised in the subprocess are not propagated to the main thread with this hack.

import subprocess

proc = subprocess.Popen(["python", "-c", f"from python_on_whales import DockerClient; docker = DockerClient(); docker.login(server='{server}', username='{username}', password='{password}')"], stdout=subprocess.PIPE)
out = proc.communicate()[0]

I also tested the behaviour of docker-py package and they do not output "Login Succeeded"

I was wondering if there was a better way to capture the output of python_on_whales methods or silence them if needed (--quiet).