From 22f147569b9248ba77b97971409fb76e2621734d Mon Sep 17 00:00:00 2001 From: Ben Firshman Date: Mon, 27 Jan 2014 15:19:16 +0000 Subject: [PATCH] Allow links to specified as (name, alias) tuples --- README.md | 5 +++++ docker/client.py | 5 ++++- tests/test.py | 25 +++++++++++++++++++++++++ 3 files changed, 34 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 1b4e9d78..b7968882 100644 --- a/README.md +++ b/README.md @@ -206,6 +206,11 @@ container ports to the host. See "Port bindings" below for more information. `lxc_conf` allows to pass LXC configuration options using a dictionary. `privileged` starts the container in privileged mode. +[Links](http://docs.docker.io/en/latest/use/working_with_links_names/) +can be specified with the `links` argument. They can either be +specified as a dictionary mapping name to alias or as a list of +`(name, alias)` tuples. + ```python c.stop(container, timeout=10) ``` diff --git a/docker/client.py b/docker/client.py index 3fc2b084..fb878ce2 100644 --- a/docker/client.py +++ b/docker/client.py @@ -705,8 +705,11 @@ class Client(requests.Session): start_config['PublishAllPorts'] = publish_all_ports if links: + if isinstance(links, dict): + links = six.iteritems(links) + formatted_links = [ - '{0}:{1}'.format(k, v) for k, v in sorted(six.iteritems(links)) + '{0}:{1}'.format(k, v) for k, v in sorted(links) ] start_config['Links'] = formatted_links diff --git a/tests/test.py b/tests/test.py index d92ffddc..2bebb1cb 100644 --- a/tests/test.py +++ b/tests/test.py @@ -525,6 +525,31 @@ class DockerClientTest(unittest.TestCase): {'Content-Type': 'application/json'} ) + def test_start_container_with_links_as_list_of_tuples(self): + # one link + try: + link_path = 'path' + alias = 'alias' + self.client.start(fake_api.FAKE_CONTAINER_ID, + links=[(link_path, alias)]) + except Exception as e: + self.fail('Command should not raise exception: {0}'.format(e)) + + args = fake_request.call_args + self.assertEqual( + args[0][0], + 'unix://var/run/docker.sock/v1.6/containers/3cc2351ab11b/start' + ) + self.assertEqual( + json.loads(args[1]['data']), + {"PublishAllPorts": False, "Privileged": False, + "Links": ["path:alias"]} + ) + self.assertEqual( + args[1]['headers'], + {'Content-Type': 'application/json'} + ) + def test_start_container_privileged(self): try: self.client.start(fake_api.FAKE_CONTAINER_ID, privileged=True)