| 262 | _pull(service) |
| 263 | |
| 264 | def build(self, service_name, use_cache=True, use_leaf_cache=True, |
| 265 | pull_parents=True): |
| 266 | def _build(service, use_cache): |
| 267 | if 'build' not in service: |
| 268 | # nothing to do |
| 269 | return |
| 270 | |
| 271 | args = [] |
| 272 | cache_from = list(service.get('build', {}).get('cache_from', [])) |
| 273 | if pull_parents: |
| 274 | for image in cache_from: |
| 275 | if image not in self.pull_memory: |
| 276 | try: |
| 277 | self._execute_docker('pull', image) |
| 278 | except Exception as e: |
| 279 | print(e) |
| 280 | finally: |
| 281 | self.pull_memory.add(image) |
| 282 | |
| 283 | if not use_cache: |
| 284 | args.append('--no-cache') |
| 285 | |
| 286 | # turn on inline build cache, this is a docker buildx feature |
| 287 | # used to bundle the image build cache to the pushed image manifest |
| 288 | # so the build cache can be reused across hosts, documented at |
| 289 | # https://github.com/docker/buildx#--cache-tonametypetypekeyvalue |
| 290 | if self.config.env.get('BUILDKIT_INLINE_CACHE') == '1': |
| 291 | args.extend(['--build-arg', 'BUILDKIT_INLINE_CACHE=1']) |
| 292 | |
| 293 | if self.config.using_buildx: |
| 294 | for k, v in service['build'].get('args', {}).items(): |
| 295 | args.extend(['--build-arg', f'{k}={v}']) |
| 296 | |
| 297 | if use_cache: |
| 298 | cache_ref = f'{service["image"]}-cache' |
| 299 | cache_from = f'type=registry,ref={cache_ref}' |
| 300 | cache_to = f'type=registry,ref={cache_ref},mode=max' |
| 301 | args.extend([ |
| 302 | '--cache-from', cache_from, |
| 303 | '--cache-to', cache_to, |
| 304 | ]) |
| 305 | |
| 306 | args.extend([ |
| 307 | '--output', 'type=docker', |
| 308 | '-f', arrow_path(service['build']['dockerfile']), |
| 309 | '-t', service['image'], |
| 310 | service['build'].get('context', '.') |
| 311 | ]) |
| 312 | self._execute_docker("buildx", "build", *args) |
| 313 | elif self.config.using_docker: |
| 314 | # better for caching |
| 315 | if self.config.debug and os.name != "nt": |
| 316 | args.append("--progress=plain") |
| 317 | for k, v in service['build'].get('args', {}).items(): |
| 318 | args.extend(['--build-arg', f'{k}={v}']) |
| 319 | for img in cache_from: |
| 320 | args.append(f'--cache-from="{img}"') |
| 321 | args.extend([ |