Skip to content

Commit 9c905f9

Browse files
authored
Merge pull request containers#1116 from Zeglius/dockerfile_inline
Add support for dockerfile_inline
2 parents d79ff01 + 105e390 commit 9c905f9

File tree

3 files changed

+72
-15
lines changed

3 files changed

+72
-15
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
version: '3'
3+
services:
4+
dummy:
5+
build:
6+
context: .
7+
dockerfile_inline: |
8+
FROM alpine
9+
RUN echo "hello world"

podman_compose.py

+39-15
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import signal
2626
import subprocess
2727
import sys
28+
import tempfile
2829
from asyncio import Task
2930
from enum import Enum
3031

@@ -2471,27 +2472,45 @@ async def compose_push(compose, args):
24712472
await compose.podman.run([], "push", [cnt["image"]])
24722473

24732474

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):
24752476
build_desc = cnt["build"]
24762477
if not hasattr(build_desc, "items"):
24772478
build_desc = {"context": build_desc}
24782479
ctx = build_desc.get("context", ".")
24792480
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)
24822498
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:
24922500
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
24952514
if not path_exists(dockerfile):
24962515
raise OSError("Dockerfile not found in " + ctx)
24972516
build_args = ["-f", dockerfile, "-t", cnt["image"]]
@@ -2546,8 +2565,13 @@ async def build_one(compose, args, cnt):
25462565
if img_id:
25472566
return None
25482567

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+
)
25502572
status = await compose.podman.run([], "build", build_args)
2573+
for c in cleanup_callbacks:
2574+
c()
25512575
return status
25522576

25532577

tests/unit/test_container_to_build_args.py

+24
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# SPDX-License-Identifier: GPL-2.0
22

3+
import os
34
import unittest
45
from unittest import mock
56

@@ -156,3 +157,26 @@ def test_caches(self):
156157
'.',
157158
],
158159
)
160+
161+
def test_dockerfile_inline(self):
162+
c = create_compose_mock()
163+
164+
cnt = get_minimal_container()
165+
cnt['build']['dockerfile_inline'] = "FROM busybox\nRUN echo 'hello world'"
166+
args = get_minimal_args()
167+
168+
cleanup_callbacks = []
169+
args = container_to_build_args(
170+
c, cnt, args, lambda path: True, cleanup_callbacks=cleanup_callbacks
171+
)
172+
173+
temp_dockerfile = args[args.index("-f") + 1]
174+
self.assertTrue(os.path.exists(temp_dockerfile))
175+
176+
with open(temp_dockerfile, "rt") as file:
177+
contents = file.read()
178+
self.assertEqual(contents, "FROM busybox\n" + "RUN echo 'hello world'")
179+
180+
for c in cleanup_callbacks:
181+
c()
182+
self.assertFalse(os.path.exists(temp_dockerfile))

0 commit comments

Comments
 (0)