From c2a145c2d8d3eac63f9a4126c9043d50f8597dbc Mon Sep 17 00:00:00 2001 From: Enrico Date: Fri, 5 Jul 2013 15:25:52 -0300 Subject: [PATCH] Added ability to create binds when starting a container. --- docker/client.py | 13 +++++++++++-- tests/test.py | 25 +++++++++++++++++++++++-- 2 files changed, 34 insertions(+), 4 deletions(-) diff --git a/docker/client.py b/docker/client.py index e48ee1ba..93a82d3a 100644 --- a/docker/client.py +++ b/docker/client.py @@ -280,10 +280,19 @@ class Client(requests.Session): return self._result(self.get(self._url("/images/search"), params={'term': term}), True) - def start(self, *args): + def start(self, *args, **kwargs): + start_config = {} + binds = kwargs.pop('binds', '') + if binds: + bind_pairs = ['{0}:{1}'.format(host, dest) for host, dest in binds.items()] + start_config = { + 'Binds': bind_pairs, + } + for name in args: url = self._url("/containers/{0}/start".format(name)) - self.post(url, None) + self.post_json( + url, start_config, headers={"Content-Type":"application/json"}) def stop(self, *args, **kwargs): params = { diff --git a/tests/test.py b/tests/test.py index dce9f766..89326783 100644 --- a/tests/test.py +++ b/tests/test.py @@ -1,5 +1,5 @@ +import os import unittest -import time import docker @@ -97,6 +97,27 @@ class TestCreateContainer(BaseTestCase): self.assertIn('Id', res) self.tmp_containers.append(res['Id']) +class TestCreateContainerWithBinds(BaseTestCase): + def runTest(self): + mount_dest = '/mnt' + mount_origin = os.getcwd() + + filename = 'shared.txt' + shared_file = os.path.join(mount_origin, filename) + + with open(shared_file, 'w'): + container = self.client.create_container('busybox', + ['ls', mount_dest], volumes={mount_dest: {}}) + container_id = container['Id'] + self.client.start(container_id, binds={mount_origin: mount_dest}) + self.tmp_containers.append(container_id) + exitcode = self.client.wait(container_id) + self.assertEqual(exitcode, 0) + logs = self.client.logs(container_id) + + os.unlink(shared_file) + self.assertIn(filename, logs) + class TestStartContainer(BaseTestCase): def runTest(self): res = self.client.create_container('busybox', 'true') @@ -297,4 +318,4 @@ class TestRunShlex(BaseTestCase): if __name__ == '__main__': - unittest.main() \ No newline at end of file + unittest.main()