Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Display stack trace when tty is enabled #2463

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 4 additions & 1 deletion docker/models/containers.py
Original file line number Diff line number Diff line change
Expand Up @@ -823,7 +823,10 @@ def run(self, image, command=None, stdout=True, stderr=False,
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()
Expand Down
9 changes: 9 additions & 0 deletions tests/integration/models_containers_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,15 @@ def test_run_with_error(self):
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):
Expand Down
10 changes: 10 additions & 0 deletions tests/unit/models_containers_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,16 @@ def test_run_with_error(self):
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)
Expand Down