Skip to content

Commit 6b59dc6

Browse files
davidsteinesshin-
authored andcommitted
Allow detach and remove for api version >= 1.25 and use auto_remove when both are set. Continue raising an exception for api versions <1.25.
Signed-off-by: David Steines <[email protected]>
1 parent 2a6926b commit 6b59dc6

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

docker/models/containers.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from ..errors import (ContainerError, ImageNotFound,
55
create_unexpected_kwargs_error)
66
from ..types import HostConfig
7+
from ..utils import compare_version
78
from .images import Image
89
from .resource import Collection, Model
910

@@ -690,8 +691,12 @@ def run(self, image, command=None, stdout=True, stderr=False,
690691
image = image.id
691692
detach = kwargs.pop("detach", False)
692693
if detach and remove:
693-
raise RuntimeError("The options 'detach' and 'remove' cannot be "
694-
"used together.")
694+
if compare_version("1.24",
695+
self.client.api._version) > 0:
696+
kwargs["auto_remove"] = True
697+
else:
698+
raise RuntimeError("The options 'detach' and 'remove' cannot "
699+
"be used together in api versions < 1.25.")
695700

696701
if kwargs.get('network') and kwargs.get('network_mode'):
697702
raise RuntimeError(
@@ -849,6 +854,7 @@ def prune(self, filters=None):
849854

850855
# kwargs to copy straight from run to host_config
851856
RUN_HOST_CONFIG_KWARGS = [
857+
'auto_remove',
852858
'blkio_weight_device',
853859
'blkio_weight',
854860
'cap_add',

tests/unit/models_containers_test.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,9 +273,39 @@ def test_run_remove(self):
273273
client.api.remove_container.assert_called_with(FAKE_CONTAINER_ID)
274274

275275
client = make_fake_client()
276+
client.api._version = '1.24'
276277
with self.assertRaises(RuntimeError):
277278
client.containers.run("alpine", detach=True, remove=True)
278279

280+
client = make_fake_client()
281+
client.api._version = '1.23'
282+
with self.assertRaises(RuntimeError):
283+
client.containers.run("alpine", detach=True, remove=True)
284+
285+
client = make_fake_client()
286+
client.api._version = '1.25'
287+
client.containers.run("alpine", detach=True, remove=True)
288+
client.api.remove_container.assert_not_called()
289+
client.api.create_container.assert_called_with(
290+
command=None,
291+
image='alpine',
292+
detach=True,
293+
host_config={'AutoRemove': True,
294+
'NetworkMode': 'default'}
295+
)
296+
297+
client = make_fake_client()
298+
client.api._version = '1.26'
299+
client.containers.run("alpine", detach=True, remove=True)
300+
client.api.remove_container.assert_not_called()
301+
client.api.create_container.assert_called_with(
302+
command=None,
303+
image='alpine',
304+
detach=True,
305+
host_config={'AutoRemove': True,
306+
'NetworkMode': 'default'}
307+
)
308+
279309
def test_create(self):
280310
client = make_fake_client()
281311
container = client.containers.create(

0 commit comments

Comments
 (0)