Skip to content

Commit

Permalink
Merge branch 'davidsteines-1477_detach_and_remove_together'
Browse files Browse the repository at this point in the history
  • Loading branch information
shin- committed Aug 16, 2017
2 parents e9fab14 + b78fa64 commit 67b58b8
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
9 changes: 7 additions & 2 deletions docker/models/containers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from ..errors import (ContainerError, ImageNotFound,
create_unexpected_kwargs_error)
from ..types import HostConfig
from ..utils import version_gte
from .images import Image
from .resource import Collection, Model

Expand Down Expand Up @@ -690,8 +691,11 @@ def run(self, image, command=None, stdout=True, stderr=False,
image = image.id
detach = kwargs.pop("detach", False)
if detach and remove:
raise RuntimeError("The options 'detach' and 'remove' cannot be "
"used together.")
if version_gte(self.client.api._version, '1.25'):
kwargs["auto_remove"] = True
else:
raise RuntimeError("The options 'detach' and 'remove' cannot "
"be used together in api versions < 1.25.")

if kwargs.get('network') and kwargs.get('network_mode'):
raise RuntimeError(
Expand Down Expand Up @@ -849,6 +853,7 @@ def prune(self, filters=None):

# kwargs to copy straight from run to host_config
RUN_HOST_CONFIG_KWARGS = [
'auto_remove',
'blkio_weight_device',
'blkio_weight',
'cap_add',
Expand Down
30 changes: 30 additions & 0 deletions tests/unit/models_containers_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,9 +273,39 @@ def test_run_remove(self):
client.api.remove_container.assert_called_with(FAKE_CONTAINER_ID)

client = make_fake_client()
client.api._version = '1.24'
with self.assertRaises(RuntimeError):
client.containers.run("alpine", detach=True, remove=True)

client = make_fake_client()
client.api._version = '1.23'
with self.assertRaises(RuntimeError):
client.containers.run("alpine", detach=True, remove=True)

client = make_fake_client()
client.api._version = '1.25'
client.containers.run("alpine", detach=True, remove=True)
client.api.remove_container.assert_not_called()
client.api.create_container.assert_called_with(
command=None,
image='alpine',
detach=True,
host_config={'AutoRemove': True,
'NetworkMode': 'default'}
)

client = make_fake_client()
client.api._version = '1.26'
client.containers.run("alpine", detach=True, remove=True)
client.api.remove_container.assert_not_called()
client.api.create_container.assert_called_with(
command=None,
image='alpine',
detach=True,
host_config={'AutoRemove': True,
'NetworkMode': 'default'}
)

def test_create(self):
client = make_fake_client()
container = client.containers.create(
Expand Down

0 comments on commit 67b58b8

Please sign in to comment.