فهرست منبع

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.
Ben Firshman 12 سال پیش
والد
کامیت
507940535f
2فایلهای تغییر یافته به همراه26 افزوده شده و 2 حذف شده
  1. 14 2
      plum/service.py
  2. 12 0
      tests/service_test.py

+ 14 - 2
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+)$')
 

+ 12 - 0
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])