Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for dockerfile_inline #1116

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

Zeglius
Copy link

@Zeglius Zeglius commented Jan 22, 2025

Fixes #864

We dump the contents of service.*.build.dockerfile_inline in a temporary file, which gets fed to the -f flag.

Also raises errors whenever both dockerfile_inline and dockerfile are set, just like docker does, see https://docs.docker.com/reference/compose-file/build/#dockerfile_inline

@Zeglius Zeglius force-pushed the dockerfile_inline branch 3 times, most recently from e2c928b to f2492f6 Compare January 22, 2025 17:51

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

import os
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be imported at the top of the file.

dockerfile.close()
dockerfile = dockerfile.name
# Dont override the temporary dockerfile
if dockerfile_inline is None:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should just be else

dockerfile.write(dockerfile_inline.encode())
dockerfile.close()
dockerfile = dockerfile.name
# Dont override the temporary dockerfile
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Obvious comment, not necessary.

# 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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Obvious comment, not necessary.

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")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Current code does not remove the temporary file, does it? I think this is primary deficiency of the current approach. Not sure how to best solve it right now.

One solution would be to introduce a list of cleanup functions that need to be called before podman-compose exits. Then pass this list to container_to_build_args which would append an appropriate os.remove call to that list. And then after podman build completes, remove the file in e.g. finally.

Remove temporary dockerfile generated when using dockerfile_inline field
dockerfile = dockerfile.name
def cleanup_temp_dockfile():
os.remove(dockerfile) if os.path.exists(dockerfile) else None
list.append(kwargs["cleanup_callbacks"], cleanup_temp_dockfile)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not:

if cleanup_callbacks is not None:
    cleanup_callbacks.append(cleanup_temp_dockfile)

dockerfile.close()
dockerfile = dockerfile.name
def cleanup_temp_dockfile():
os.remove(dockerfile) if os.path.exists(dockerfile) else None
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since return value is not used, normal if is more readable:

if os.path.exists(dockerfile):
    os.remove(dockerfile)

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


def container_to_build_args(compose, cnt, args, path_exists):
def container_to_build_args(compose, cnt, args, path_exists, **kwargs):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better have cleanup_callbacks=None explicitly

@p12tic
Copy link
Collaborator

p12tic commented Jan 24, 2025

Serious issues have been fixes, stylistic issues is the only thing left. Thank you for looking into this feature.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support for dockerfile_inline missing
2 participants