diff --git a/docker/models/containers.py b/docker/models/containers.py index 9c9e92c9..7f1d752e 100644 --- a/docker/models/containers.py +++ b/docker/models/containers.py @@ -898,7 +898,10 @@ class ContainerCollection(Collection): if exit_status != 0: out = None if not kwargs.get('auto_remove'): - out = container.logs(stdout=False, stderr=True) + if not kwargs.get('tty'): + out = container.logs(stdout=False, stderr=True) + else: + out = container.logs(stdout=True, stderr=True) if remove: container.remove() diff --git a/tests/integration/models_containers_test.py b/tests/integration/models_containers_test.py index 87274559..33387e88 100644 --- a/tests/integration/models_containers_test.py +++ b/tests/integration/models_containers_test.py @@ -34,6 +34,15 @@ class ContainerCollectionTest(BaseIntegrationTest): assert "alpine" in cm.exconly() assert "No such file or directory" in cm.exconly() + def test_run_with_error_with_tty(self): + client = docker.from_env(version=TEST_API_VERSION) + with pytest.raises(docker.errors.ContainerError) as cm: + client.containers.run("alpine", "cat /test", remove=True, tty=True) + assert cm.value.exit_status == 1 + assert "cat /test" in cm.exconly() + assert "alpine" in cm.exconly() + assert "No such file or directory" in cm.exconly() + def test_run_with_image_that_does_not_exist(self): client = docker.from_env(version=TEST_API_VERSION) with pytest.raises(docker.errors.ImageNotFound): diff --git a/tests/unit/models_containers_test.py b/tests/unit/models_containers_test.py index 0e2ae341..fb759ebe 100644 --- a/tests/unit/models_containers_test.py +++ b/tests/unit/models_containers_test.py @@ -270,6 +270,16 @@ class ContainerCollectionTest(unittest.TestCase): assert cm.value.exit_status == 1 assert "some error" in cm.exconly() + def test_run_with_error_and_tty(self): + client = make_fake_client() + client.api.logs.return_value = "some error" + client.api.wait.return_value = {'StatusCode': 1} + + with pytest.raises(docker.errors.ContainerError) as cm: + client.containers.run('alpine', 'echo hello world', tty=True) + assert cm.value.exit_status == 1 + assert "some error" in cm.exconly() + def test_run_with_image_object(self): client = make_fake_client() image = client.images.get(FAKE_IMAGE_ID)