Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
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,6 +823,8 @@ def run(self, image, command=None, stdout=True, stderr=False,

volumes_from (:py:class:`list`): List of container names or IDs to
get volumes from.
wait_condition (dict): A dictionary to configure the wait mechanism.
Passed directly to the :py:meth:`Container.wait` method.
working_dir (str): Path to the working directory.

Returns:
Expand Down Expand Up @@ -853,6 +855,7 @@ def run(self, image, command=None, stdout=True, stderr=False,
stream = kwargs.pop('stream', False)
detach = kwargs.pop('detach', False)
platform = kwargs.get('platform', None)
waitcond = kwargs.pop('wait_condition', {})

if detach and remove:
if version_gte(self.client.api._version, '1.25'):
Expand Down Expand Up @@ -894,7 +897,7 @@ def run(self, image, command=None, stdout=True, stderr=False,
stdout=stdout, stderr=stderr, stream=True, follow=True
)

exit_status = container.wait()['StatusCode']
exit_status = container.wait(**waitcond)['StatusCode']
if exit_status != 0:
out = None
if not kwargs.get('auto_remove'):
Expand Down
6 changes: 6 additions & 0 deletions tests/integration/models_containers_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ def test_run_detach(self):
assert container.attrs['Config']['Image'] == "alpine"
assert container.attrs['Config']['Cmd'] == ['sleep', '300']

def test_run_with_timeout(self):
client = docker.from_env(version=TEST_API_VERSION)
with pytest.raises(docker.errors.requests.exceptions.ConnectionError) as cm:
client.containers.run("alpine", "sh -c 'echo a && sleep 10 && echo b'", wait_condition={'timeout': 3})
assert "Error" in cm.exconly()

def test_run_with_error(self):
client = docker.from_env(version=TEST_API_VERSION)
with pytest.raises(docker.errors.ContainerError) as cm:
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 @@ -260,6 +260,16 @@ def test_run_pull(self):
'alpine', platform=None, tag='latest', all_tags=False, stream=True
)

def test_run_with_timeout(self):
client = make_fake_client()
client.api.logs.return_value = b"a\n"
client.api.wait.return_value = {'StatusCode': 127}

with pytest.raises(docker.errors.ContainerError) as cm:
client.containers.run('alpine', "sh -c 'echo a && sleep 10 && echo b'", wait_condition={'timeout': 3})
assert cm.value.exit_status == 127
assert 'a\\n' in cm.exconly()

def test_run_with_error(self):
client = make_fake_client()
client.api.logs.return_value = "some error"
Expand Down