Browse Source

DockerImage.build(): arg dockerfile -> dockerfile_or_path

Fabian Peter Hammerle 5 years ago
parent
commit
c67e67f0cf
1 changed files with 9 additions and 5 deletions
  1. 9 5
      rc.xsh

+ 9 - 5
rc.xsh

@@ -93,13 +93,17 @@ class DockerImage:
         return '{}(id={!r}, tags={!r})'.format(type(self).__name__, self._id, self._tags)
 
     @classmethod
-    def build(cls, dockerfile):
+    def build(cls, dockerfile_or_path):
         out = io.BytesIO()
         with StdoutTee(out) as tee:
-            p = subprocess.Popen(['sudo', 'docker', 'build', '-'],
-                                  stdin=subprocess.PIPE, stdout=tee)
-            p.stdin.write(dockerfile.encode())
-            p.stdin.close()
+            if os.path.exists(dockerfile_or_path):
+                p = subprocess.Popen(['sudo', 'docker', 'build', dockerfile_or_path],
+                                     stdin=None, stdout=tee)
+            else:
+                p = subprocess.Popen(['sudo', 'docker', 'build', '-'],
+                                     stdin=subprocess.PIPE, stdout=tee)
+                p.stdin.write(dockerfile_or_path.encode())
+                p.stdin.close()
             assert p.wait() == 0, 'docker build failed'
         image_id, = re.search(rb'^Successfully built (\S+)$', out.getvalue(), re.MULTILINE).groups()
         return cls(image_id.decode(sys.stdout.encoding))