diff --git a/docker/utils/utils.py b/docker/utils/utils.py index 403296b2..63cd2a79 100644 --- a/docker/utils/utils.py +++ b/docker/utils/utils.py @@ -454,14 +454,13 @@ def create_container_config( for k, v in six.iteritems(environment) ] - if isinstance(labels, six.string_types): - labels = [labels, ] + if labels is not None and compare_version('1.18', version) < 0: + raise errors.DockerException( + 'labels were only introduced in API version 1.18' + ) if isinstance(labels, list): - labels_dict = {} - for lbl in labels: - labels_dict[lbl] = {} - labels = labels_dict + labels = dict((lbl, six.text_type('')) for lbl in labels) if isinstance(mem_limit, six.string_types): mem_limit = parse_bytes(mem_limit) diff --git a/docs/api.md b/docs/api.md index fa1987a4..2415cc24 100644 --- a/docs/api.md +++ b/docs/api.md @@ -209,6 +209,7 @@ from. Optionally a single string joining container id's with commas * memswap_limit (int): * host_config (dict): A [HostConfig](hostconfig.md) dictionary * mac_address (str): The Mac Address to assign the container +* labels (dict or list): A dictionary of name-value labels (e.g. `{"label1": "value1", "label2": "value2"}`) or a list of names of labels to set with empty values (e.g. `["label1", "label2"]`) **Returns** (dict): A dictionary with an image 'Id' key and a 'Warnings' key. diff --git a/tests/test.py b/tests/test.py index 5750c94d..a363c351 100644 --- a/tests/test.py +++ b/tests/test.py @@ -1305,6 +1305,54 @@ class DockerClientTest(Cleanup, unittest.TestCase): args[1]['timeout'], docker.client.DEFAULT_TIMEOUT_SECONDS ) + def test_create_container_with_labels_dict(self): + labels_dict = { + six.text_type('foo'): six.text_type('1'), + six.text_type('bar'): six.text_type('2'), + } + try: + self.client.create_container( + 'busybox', 'true', + labels=labels_dict, + ) + except Exception as e: + self.fail('Command should not raise exception: {0}'.format(e)) + args = fake_request.call_args + self.assertEqual(args[0][0], url_prefix + 'containers/create') + self.assertEqual(json.loads(args[1]['data'])['Labels'], labels_dict) + self.assertEqual( + args[1]['headers'], {'Content-Type': 'application/json'} + ) + self.assertEqual( + args[1]['timeout'], docker.client.DEFAULT_TIMEOUT_SECONDS + ) + + def test_create_container_with_labels_list(self): + labels_list = [ + six.text_type('foo'), + six.text_type('bar'), + ] + labels_dict = { + six.text_type('foo'): six.text_type(), + six.text_type('bar'): six.text_type(), + } + try: + self.client.create_container( + 'busybox', 'true', + labels=labels_list, + ) + except Exception as e: + self.fail('Command should not raise exception: {0}'.format(e)) + args = fake_request.call_args + self.assertEqual(args[0][0], url_prefix + 'containers/create') + self.assertEqual(json.loads(args[1]['data'])['Labels'], labels_dict) + self.assertEqual( + args[1]['headers'], {'Content-Type': 'application/json'} + ) + self.assertEqual( + args[1]['timeout'], docker.client.DEFAULT_TIMEOUT_SECONDS + ) + def test_resize_container(self): try: self.client.resize(