Skip to content

Commit

Permalink
Add support for dockerfile_inline
Browse files Browse the repository at this point in the history
Fixes #864

Signed-off-by: Zeglius <[email protected]>
  • Loading branch information
Zeglius committed Jan 22, 2025
1 parent f376700 commit da10fdf
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 14 deletions.
9 changes: 9 additions & 0 deletions examples/docker-inline/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
version: '3'
services:
dummy:
build:
context: .
dockerfile_inline: |
FROM alpine
RUN echo "hello world"
42 changes: 28 additions & 14 deletions podman_compose.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import signal
import subprocess
import sys
import tempfile
from asyncio import Task
from enum import Enum

Expand Down Expand Up @@ -2477,21 +2478,34 @@ def container_to_build_args(compose, cnt, args, path_exists):
build_desc = {"context": build_desc}
ctx = build_desc.get("context", ".")
dockerfile = build_desc.get("dockerfile")
if dockerfile:
dockerfile = os.path.join(ctx, dockerfile)
else:
dockerfile_alts = [
"Containerfile",
"ContainerFile",
"containerfile",
"Dockerfile",
"DockerFile",
"dockerfile",
]
for dockerfile in dockerfile_alts:
dockerfile_inline = build_desc.get("dockerfile_inline")
if dockerfile_inline is not None:
dockerfile_inline = str(dockerfile_inline)
# Error if both `dockerfile_inline` and `dockerfile` are set
if dockerfile and dockerfile_inline:
raise OSError("dockerfile_inline and dockerfile can't be used simultaneously")
# Create a temporary dockerfile
dockerfile = tempfile.NamedTemporaryFile(delete=False, suffix=".containerfile")
dockerfile.write(dockerfile_inline.encode())
dockerfile.close()
dockerfile = dockerfile.name
# Dont override the temporary dockerfile
if dockerfile_inline is None:
if dockerfile:
dockerfile = os.path.join(ctx, dockerfile)
if path_exists(dockerfile):
break
else:
dockerfile_alts = [
"Containerfile",
"ContainerFile",
"containerfile",
"Dockerfile",
"DockerFile",
"dockerfile",
]
for dockerfile in dockerfile_alts:
dockerfile = os.path.join(ctx, dockerfile)
if path_exists(dockerfile):
break
if not path_exists(dockerfile):
raise OSError("Dockerfile not found in " + ctx)
build_args = ["-f", dockerfile, "-t", cnt["image"]]
Expand Down
18 changes: 18 additions & 0 deletions tests/unit/test_container_to_build_args.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,3 +156,21 @@ def test_caches(self):
'.',
],
)

def test_dockerfile_inline(self):
c = create_compose_mock()

cnt = get_minimal_container()
cnt['build']['dockerfile_inline'] = "FROM busybox\nRUN echo 'hello world'"
args = get_minimal_args()

args = container_to_build_args(c, cnt, args, lambda path: True)

import os

temp_dockerfile = args[args.index("-f") + 1]
self.assertTrue(os.path.exists(temp_dockerfile))

with open(temp_dockerfile, "rt") as file:
contents = file.read()
self.assertEqual(contents, "FROM busybox\n" + "RUN echo 'hello world'")

0 comments on commit da10fdf

Please sign in to comment.