From 507940535fbb8f3932287dfd313ce01d99354abe Mon Sep 17 00:00:00 2001 From: Ben Firshman Date: Fri, 20 Dec 2013 16:23:40 +0000 Subject: [PATCH] Tag built images and use them when starting A basic measure to get round the fact that adding isn't cached. Once Docker supports cached adds, this is probably redundant. --- plum/service.py | 16 ++++++++++++++-- tests/service_test.py | 12 ++++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/plum/service.py b/plum/service.py index 6fab273b6..0550a7c03 100644 --- a/plum/service.py +++ b/plum/service.py @@ -139,14 +139,20 @@ class Service(object): container_options['volumes'] = dict((v.split(':')[1], {}) for v in container_options['volumes']) if 'build' in self.options: - container_options['image'] = self.build() + if len(self.client.images(name=self._build_tag_name())) == 0: + self.build() + container_options['image'] = self._build_tag_name() return container_options def build(self): log.info('Building %s...' % self.name) - build_output = self.client.build(self.options['build'], stream=True) + build_output = self.client.build( + self.options['build'], + tag=self._build_tag_name(), + stream=True + ) image_id = None @@ -162,6 +168,12 @@ class Service(object): return image_id + def _build_tag_name(self): + """ + The tag to give to images built for this service. + """ + return '%s_%s' % (self.project, self.name) + NAME_RE = re.compile(r'^([^_]+)_([^_]+)_(run_)?(\d+)$') diff --git a/tests/service_test.py b/tests/service_test.py index 2fdfba7de..3f84664dc 100644 --- a/tests/service_test.py +++ b/tests/service_test.py @@ -113,6 +113,18 @@ class ServiceTest(DockerClientTestCase): container = service.start() container.wait() self.assertIn('success', container.logs()) + self.assertEqual(len(self.client.images(name='default_test')), 1) + + def test_start_container_uses_tagged_image_if_it_exists(self): + self.client.build('tests/fixtures/simple-dockerfile', tag='default_test') + service = Service( + name='test', + client=self.client, + build='this/does/not/exist/and/will/throw/error', + ) + container = service.start() + container.wait() + self.assertIn('success', container.logs()) def test_start_container_creates_ports(self): service = self.create_service('web', ports=[8000])