Repository: docker/docker-py

Issue:
How to get exec_run exit_code?
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.


Discussion:
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`.

Resolving patch:
diff --git a/docker/models/containers.py b/docker/models/containers.py
--- a/docker/models/containers.py
+++ b/docker/models/containers.py
@@ -149,10 +149,14 @@ def exec_run(self, cmd, stdout=True, stderr=True, stdin=False, tty=False,
                 ``{"PASSWORD": "xxx"}``.
 
         Returns:
-            (generator or str):
-                If ``stream=True``, a generator yielding response chunks.
-                If ``socket=True``, a socket object for the connection.
-                A string containing response data otherwise.
+            dict:
+                output: (generator or str):
+                    If ``stream=True``, a generator yielding response chunks.
+                    If ``socket=True``, a socket object for the connection.
+                    A string containing response data otherwise.
+                exit_code: (int):
+                    Exited code of execution
+
         Raises:
             :py:class:`docker.errors.APIError`
                 If the server returns an error.
@@ -161,9 +165,16 @@ def exec_run(self, cmd, stdout=True, stderr=True, stdin=False, tty=False,
             self.id, cmd, stdout=stdout, stderr=stderr, stdin=stdin, tty=tty,
             privileged=privileged, user=user, environment=environment
         )
-        return self.client.api.exec_start(
+        exec_output = self.client.api.exec_start(
             resp['Id'], detach=detach, tty=tty, stream=stream, socket=socket
         )
+        exit_code = 0
+        if stream is False:
+            exit_code = self.client.api.exec_inspect(resp['Id'])['ExitCode']
+        return {
+            'exit_code': exit_code,
+            'output': exec_output
+        }
 
     def export(self):
         """


Test patch:
diff --git a/tests/integration/models_containers_test.py b/tests/integration/models_containers_test.py
--- a/tests/integration/models_containers_test.py
+++ b/tests/integration/models_containers_test.py
@@ -153,13 +153,24 @@ def test_diff(self):
         container.wait()
         assert container.diff() == [{'Path': '/test', 'Kind': 1}]
 
-    def test_exec_run(self):
+    def test_exec_run_success(self):
         client = docker.from_env(version=TEST_API_VERSION)
         container = client.containers.run(
             "alpine", "sh -c 'echo \"hello\" > /test; sleep 60'", detach=True
         )
         self.tmp_containers.append(container.id)
-        assert container.exec_run("cat /test") == b"hello\n"
+        exec_output = container.exec_run("cat /test")
+        assert exec_output["output"] == b"hello\n"
+        assert exec_output["exit_code"] == 0
+
+    def test_exec_run_failed(self):
+        client = docker.from_env(version=TEST_API_VERSION)
+        container = client.containers.run(
+            "alpine", "sh -c 'sleep 60'", detach=True
+        )
+        self.tmp_containers.append(container.id)
+        exec_output = container.exec_run("docker ps")
+        assert exec_output["exit_code"] == 126
 
     def test_kill(self):
         client = docker.from_env(version=TEST_API_VERSION)
diff --git a/tests/unit/models_containers_test.py b/tests/unit/models_containers_test.py
--- a/tests/unit/models_containers_test.py
+++ b/tests/unit/models_containers_test.py
@@ -400,6 +400,17 @@ def test_exec_run(self):
         client.api.exec_start.assert_called_with(
             FAKE_EXEC_ID, detach=False, tty=False, stream=True, socket=False
         )
+        container.exec_run("docker ps", privileged=True, stream=False)
+        client.api.exec_create.assert_called_with(
+            FAKE_CONTAINER_ID, "docker ps", stdout=True, stderr=True,
+            stdin=False, tty=False, privileged=True, user='', environment=None
+        )
+        client.api.exec_start.assert_called_with(
+            FAKE_EXEC_ID, detach=False, tty=False, stream=False, socket=False
+        )
+        client.api.exec_start.assert_called_with(
+            FAKE_EXEC_ID, detach=False, tty=False, stream=False, socket=False
+        )
 
     def test_export(self):
         client = make_fake_client()