From f344d8e65f616916386bb1e3147493ba5ae4ef05 Mon Sep 17 00:00:00 2001 From: Maxime Petazzoni Date: Wed, 6 Nov 2013 16:55:28 -0800 Subject: [PATCH 1/2] Support naming containers when creating them Add support for passing in the name parameter to the call to /containers/create to name the created container (API v1.6 feature). Signed-off-by: Maxime Petazzoni --- README.md | 4 +++- docker/client.py | 14 +++++++++----- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 37e418e2..9202e189 100644 --- a/README.md +++ b/README.md @@ -26,7 +26,9 @@ Identical to the `docker ps` command. * `c.copy(container, resource)` Identical to the `docker cp` command. -* `c.create_container(image, command=None, hostname=None, user=None, detach=False,stdin_open=False, tty=False, mem_limit=0, ports=None, environment=None, dns=None,volumes=None, volumes_from=None, privileged=False)` +* c.create_container(image, command=None, hostname=None, user=None, detach=False, + stdin_open=False, tty=False, mem_limit=0, ports=None, environment=None, + dns=None, volumes=None, volumes_from=None, privileged=False, name=None) Creates a container that can then be `start`ed. Parameters are similar to those for the `docker run` command except it doesn't support the attach options (`-a`) diff --git a/docker/client.py b/docker/client.py index 149bad95..69771d3f 100644 --- a/docker/client.py +++ b/docker/client.py @@ -125,7 +125,7 @@ class Client(requests.Session): 'Image': image, 'Volumes': volumes, 'VolumesFrom': volumes_from, - 'Privileged': privileged, + 'Privileged': privileged, } def _post_json(self, url, data, **kwargs): @@ -242,17 +242,21 @@ class Client(requests.Session): def create_container(self, image, command=None, hostname=None, user=None, detach=False, stdin_open=False, tty=False, mem_limit=0, ports=None, environment=None, dns=None, - volumes=None, volumes_from=None, privileged=False): + volumes=None, volumes_from=None, privileged=False, + name=None): config = self._container_config( image, command, hostname, user, detach, stdin_open, tty, mem_limit, ports, environment, dns, volumes, volumes_from, privileged ) - return self.create_container_from_config(config) + return self.create_container_from_config(config, name) - def create_container_from_config(self, config): + def create_container_from_config(self, config, name=None): u = self._url("/containers/create") - res = self._post_json(u, config) + params = { + 'name': name + } + res = self._post_json(u, config, params=params) return self._result(res, True) def diff(self, container): From afb349d2576af8e1373f36afa1f0fdc14b860db0 Mon Sep 17 00:00:00 2001 From: Maxime Petazzoni Date: Thu, 7 Nov 2013 15:37:41 -0800 Subject: [PATCH 2/2] Add integration test for named container creation Signed-off-by: Maxime Petazzoni --- tests/integration_test.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/tests/integration_test.py b/tests/integration_test.py index bc722f9b..fd77c930 100644 --- a/tests/integration_test.py +++ b/tests/integration_test.py @@ -30,7 +30,7 @@ class BaseTestCase(unittest.TestCase): tmp_containers = [] def setUp(self): - self.client = docker.Client() + self.client = docker.Client(version="1.6") self.client.pull('busybox') self.tmp_imgs = [] self.tmp_containers = [] @@ -168,6 +168,16 @@ class TestCreateContainerPrivileged(BaseTestCase): self.assertEqual(inspect['Config']['Privileged'], True) +class TestCreateContainerWithName(BaseTestCase): + def runTest(self): + res = self.client.create_container('busybox', 'true', name='foobar') + self.assertIn('Id', res) + self.tmp_containers.append(res['Id']) + inspect = self.client.inspect_container(res['Id']) + self.assertIn('Name', inspect) + self.assertEqual('/foobar', inspect['Name']) + + class TestStartContainer(BaseTestCase): def runTest(self): res = self.client.create_container('busybox', 'true')