mirror of https://github.com/docker/docker-py.git
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 <d.steines@gmail.com>
This commit is contained in:
parent
2a6926b5ab
commit
6b59dc6271
|
@ -4,6 +4,7 @@ from ..api import APIClient
|
||||||
from ..errors import (ContainerError, ImageNotFound,
|
from ..errors import (ContainerError, ImageNotFound,
|
||||||
create_unexpected_kwargs_error)
|
create_unexpected_kwargs_error)
|
||||||
from ..types import HostConfig
|
from ..types import HostConfig
|
||||||
|
from ..utils import compare_version
|
||||||
from .images import Image
|
from .images import Image
|
||||||
from .resource import Collection, Model
|
from .resource import Collection, Model
|
||||||
|
|
||||||
|
@ -690,8 +691,12 @@ class ContainerCollection(Collection):
|
||||||
image = image.id
|
image = image.id
|
||||||
detach = kwargs.pop("detach", False)
|
detach = kwargs.pop("detach", False)
|
||||||
if detach and remove:
|
if detach and remove:
|
||||||
raise RuntimeError("The options 'detach' and 'remove' cannot be "
|
if compare_version("1.24",
|
||||||
"used together.")
|
self.client.api._version) > 0:
|
||||||
|
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'):
|
if kwargs.get('network') and kwargs.get('network_mode'):
|
||||||
raise RuntimeError(
|
raise RuntimeError(
|
||||||
|
@ -849,6 +854,7 @@ RUN_CREATE_KWARGS = [
|
||||||
|
|
||||||
# kwargs to copy straight from run to host_config
|
# kwargs to copy straight from run to host_config
|
||||||
RUN_HOST_CONFIG_KWARGS = [
|
RUN_HOST_CONFIG_KWARGS = [
|
||||||
|
'auto_remove',
|
||||||
'blkio_weight_device',
|
'blkio_weight_device',
|
||||||
'blkio_weight',
|
'blkio_weight',
|
||||||
'cap_add',
|
'cap_add',
|
||||||
|
|
|
@ -273,9 +273,39 @@ class ContainerCollectionTest(unittest.TestCase):
|
||||||
client.api.remove_container.assert_called_with(FAKE_CONTAINER_ID)
|
client.api.remove_container.assert_called_with(FAKE_CONTAINER_ID)
|
||||||
|
|
||||||
client = make_fake_client()
|
client = make_fake_client()
|
||||||
|
client.api._version = '1.24'
|
||||||
with self.assertRaises(RuntimeError):
|
with self.assertRaises(RuntimeError):
|
||||||
client.containers.run("alpine", detach=True, remove=True)
|
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):
|
def test_create(self):
|
||||||
client = make_fake_client()
|
client = make_fake_client()
|
||||||
container = client.containers.create(
|
container = client.containers.create(
|
||||||
|
|
Loading…
Reference in New Issue