Hi,
I create an ubuntu container and then running exec_run later. But I can not get the exit_code. exec_run just return output bytecode. I don't know whether the executing result is successful or not.
>>> import docker
>>> client = docker.from_env()
>>> container = client.containers.run('ubuntu', command=['/bin/bash'], tty=True, detach=True)
>>> container.exec_run(['not_exist'])
b'rpc error: code = 2 desc = oci runtime error: exec failed: exec: "not_exist": executable file not found in $PATH\r\n'
>>>
I can use APIClient.exec_create(), APIClient.exec_start() and APIClient.exec_inspect() to achieve the goal. Is there has any high level api to use?
Thanks for your reply.
I also encountered this issue.
It appears there is no high-level API for this
Yes, this is something we need to figure out. Thanks for bringing it to our attention.
Opened PR #1495
imo, with the next breaking release Container.exec_run should return two values in that order: the exit code and the tty output, if stream is False.
Hm, but how to get exit code if do want a stream? It seems, from reading the code, that currently you can only get exit code without a stream?
@mitar Use the low-level API for this. There's no way for the client to detect reliably when the exec command has stopped running in an asynchronous manner
Hope this helps:
exec_handle = docker_client.exec_create(...)
stream = docker_client.exec_start(exec_handle, stream=True)
# Stream handling
docker_client.exec_inspect(exec_handler['Id']).get('ExitCode')
meanwhile the API provides Container.exec_run that is solving the problem.
Most helpful comment
imo, with the next breaking release
Container.exec_runshould return two values in that order: the exit code and the tty output, ifstreamisFalse.