|
25 | 25 | import signal
|
26 | 26 | import subprocess
|
27 | 27 | import sys
|
| 28 | +import tempfile |
28 | 29 | from asyncio import Task
|
29 | 30 | from enum import Enum
|
30 | 31 |
|
@@ -2471,27 +2472,45 @@ async def compose_push(compose, args):
|
2471 | 2472 | await compose.podman.run([], "push", [cnt["image"]])
|
2472 | 2473 |
|
2473 | 2474 |
|
2474 |
| -def container_to_build_args(compose, cnt, args, path_exists): |
| 2475 | +def container_to_build_args(compose, cnt, args, path_exists, cleanup_callbacks=None): |
2475 | 2476 | build_desc = cnt["build"]
|
2476 | 2477 | if not hasattr(build_desc, "items"):
|
2477 | 2478 | build_desc = {"context": build_desc}
|
2478 | 2479 | ctx = build_desc.get("context", ".")
|
2479 | 2480 | dockerfile = build_desc.get("dockerfile")
|
2480 |
| - if dockerfile: |
2481 |
| - dockerfile = os.path.join(ctx, dockerfile) |
| 2481 | + dockerfile_inline = build_desc.get("dockerfile_inline") |
| 2482 | + if dockerfile_inline is not None: |
| 2483 | + dockerfile_inline = str(dockerfile_inline) |
| 2484 | + # Error if both `dockerfile_inline` and `dockerfile` are set |
| 2485 | + if dockerfile and dockerfile_inline: |
| 2486 | + raise OSError("dockerfile_inline and dockerfile can't be used simultaneously") |
| 2487 | + dockerfile = tempfile.NamedTemporaryFile(delete=False, suffix=".containerfile") |
| 2488 | + dockerfile.write(dockerfile_inline.encode()) |
| 2489 | + dockerfile.close() |
| 2490 | + dockerfile = dockerfile.name |
| 2491 | + |
| 2492 | + def cleanup_temp_dockfile(): |
| 2493 | + if os.path.exists(dockerfile): |
| 2494 | + os.remove(dockerfile) |
| 2495 | + |
| 2496 | + if cleanup_callbacks is not None: |
| 2497 | + list.append(cleanup_callbacks, cleanup_temp_dockfile) |
2482 | 2498 | else:
|
2483 |
| - dockerfile_alts = [ |
2484 |
| - "Containerfile", |
2485 |
| - "ContainerFile", |
2486 |
| - "containerfile", |
2487 |
| - "Dockerfile", |
2488 |
| - "DockerFile", |
2489 |
| - "dockerfile", |
2490 |
| - ] |
2491 |
| - for dockerfile in dockerfile_alts: |
| 2499 | + if dockerfile: |
2492 | 2500 | dockerfile = os.path.join(ctx, dockerfile)
|
2493 |
| - if path_exists(dockerfile): |
2494 |
| - break |
| 2501 | + else: |
| 2502 | + dockerfile_alts = [ |
| 2503 | + "Containerfile", |
| 2504 | + "ContainerFile", |
| 2505 | + "containerfile", |
| 2506 | + "Dockerfile", |
| 2507 | + "DockerFile", |
| 2508 | + "dockerfile", |
| 2509 | + ] |
| 2510 | + for dockerfile in dockerfile_alts: |
| 2511 | + dockerfile = os.path.join(ctx, dockerfile) |
| 2512 | + if path_exists(dockerfile): |
| 2513 | + break |
2495 | 2514 | if not path_exists(dockerfile):
|
2496 | 2515 | raise OSError("Dockerfile not found in " + ctx)
|
2497 | 2516 | build_args = ["-f", dockerfile, "-t", cnt["image"]]
|
@@ -2546,8 +2565,13 @@ async def build_one(compose, args, cnt):
|
2546 | 2565 | if img_id:
|
2547 | 2566 | return None
|
2548 | 2567 |
|
2549 |
| - build_args = container_to_build_args(compose, cnt, args, os.path.exists) |
| 2568 | + cleanup_callbacks = [] |
| 2569 | + build_args = container_to_build_args( |
| 2570 | + compose, cnt, args, os.path.exists, cleanup_callbacks=cleanup_callbacks |
| 2571 | + ) |
2550 | 2572 | status = await compose.podman.run([], "build", build_args)
|
| 2573 | + for c in cleanup_callbacks: |
| 2574 | + c() |
2551 | 2575 | return status
|
2552 | 2576 |
|
2553 | 2577 |
|
|
0 commit comments