Skip to content

Commit 4bd3c48

Browse files
committed
Add version param to utils.create_host_config
Add create_host_config and create_container_config to Client for version awareness Trigger warning when create_host_config is used without version
1 parent 9bb6a6f commit 4bd3c48

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

docker/client.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -248,15 +248,18 @@ def create_container(self, image, command=None, hostname=None, user=None,
248248
'host_config is not supported in API < 1.15'
249249
)
250250

251-
config = utils.create_container_config(
252-
self._version, image, command, hostname, user, detach, stdin_open,
251+
config = self.create_container_config(
252+
image, command, hostname, user, detach, stdin_open,
253253
tty, mem_limit, ports, environment, dns, volumes, volumes_from,
254254
network_disabled, entrypoint, cpu_shares, working_dir, domainname,
255255
memswap_limit, cpuset, host_config, mac_address, labels,
256256
volume_driver
257257
)
258258
return self.create_container_from_config(config, name)
259259

260+
def create_container_config(self, *args, **kwargs):
261+
return utils.create_container_config(self._version, *args, **kwargs)
262+
260263
def create_container_from_config(self, config, name=None):
261264
u = self._url("/containers/create")
262265
params = {
@@ -265,6 +268,12 @@ def create_container_from_config(self, config, name=None):
265268
res = self._post_json(u, data=config, params=params)
266269
return self._result(res, True)
267270

271+
def create_host_config(self, *args, **kwargs):
272+
if not kwargs:
273+
kwargs = {}
274+
kwargs['version'] = self._version
275+
return utils.create_host_config(*args, **kwargs)
276+
268277
@check_resource
269278
def diff(self, container):
270279
return self._result(self._get(self._url("/containers/{0}/changes".
@@ -815,7 +824,7 @@ def start(self, container, binds=None, port_bindings=None, lxc_conf=None,
815824
'Please use host_config in create_container instead!',
816825
DeprecationWarning
817826
)
818-
start_config = utils.create_host_config(**start_config_kwargs)
827+
start_config = self.create_host_config(**start_config_kwargs)
819828

820829
url = self._url("/containers/{0}/start".format(container))
821830
res = self._post_json(url, data=start_config)

docker/utils/utils.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import requests
2828
import six
2929

30+
from .. import constants
3031
from .. import errors
3132
from .. import tls
3233
from .types import Ulimit, LogConfig
@@ -395,10 +396,17 @@ def create_host_config(
395396
restart_policy=None, cap_add=None, cap_drop=None, devices=None,
396397
extra_hosts=None, read_only=None, pid_mode=None, ipc_mode=None,
397398
security_opt=None, ulimits=None, log_config=None, mem_limit=None,
398-
memswap_limit=None, cgroup_parent=None
399+
memswap_limit=None, cgroup_parent=None, version=None
399400
):
400401
host_config = {}
401402

403+
if not version:
404+
warnings.warn(
405+
'docker.utils.create_host_config() is deprecated. Please use '
406+
'Client.create_host_config() instead.'
407+
)
408+
version = constants.DEFAULT_DOCKER_API_VERSION
409+
402410
if mem_limit is not None:
403411
if isinstance(mem_limit, six.string_types):
404412
mem_limit = parse_bytes(mem_limit)
@@ -433,7 +441,7 @@ def create_host_config(
433441

434442
if network_mode:
435443
host_config['NetworkMode'] = network_mode
436-
elif network_mode is None:
444+
elif network_mode is None and compare_version('1.19', version) > 0:
437445
host_config['NetworkMode'] = 'default'
438446

439447
if restart_policy:

0 commit comments

Comments
 (0)