Skip to content

Commit 598f167

Browse files
committed
Don't attempt to retrieve container's stderr if auto_remove was set
Signed-off-by: Joffrey F <[email protected]>
1 parent f10c008 commit 598f167

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

docker/models/containers.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -737,7 +737,9 @@ def run(self, image, command=None, stdout=True, stderr=False,
737737

738738
exit_status = container.wait()
739739
if exit_status != 0:
740-
out = container.logs(stdout=False, stderr=True)
740+
out = None
741+
if not kwargs.get('auto_remove'):
742+
out = container.logs(stdout=False, stderr=True)
741743

742744
if remove:
743745
container.remove()

tests/integration/models_containers_test.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
import docker
21
import tempfile
2+
3+
import docker
4+
import pytest
35
from .base import BaseIntegrationTest, TEST_API_VERSION
46
from ..helpers import random_name, requires_api_version
57

@@ -114,6 +116,16 @@ def test_run_with_auto_remove(self):
114116
)
115117
assert out == b'hello\n'
116118

119+
@requires_api_version('1.25')
120+
def test_run_with_auto_remove_error(self):
121+
client = docker.from_env(version=TEST_API_VERSION)
122+
with pytest.raises(docker.errors.ContainerError) as e:
123+
client.containers.run(
124+
'alpine', 'sh -c ">&2 echo error && exit 1"', auto_remove=True
125+
)
126+
assert e.value.exit_status == 1
127+
assert e.value.stderr is None
128+
117129
def test_run_with_streamed_logs(self):
118130
client = docker.from_env(version=TEST_API_VERSION)
119131
out = client.containers.run(

0 commit comments

Comments
 (0)