Skip to content

Add support for Containerfile #100

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

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

Conversation

bshephar
Copy link

@bshephar bshephar commented Jun 10, 2025

This change adds a function to check for either a Dockerfile or Containerfile. If either exist, and the user hasn't explicitly provided the file to use, then we can default to the existing file.

Fixes #99

@bshephar
Copy link
Author

First time working with Swift, be gentle. :)
I've left this on draft while I try to get the tests working locally.

@bshephar bshephar force-pushed the support-containerfile branch 2 times, most recently from 1860948 to 0dbb950 Compare June 10, 2025 13:40
Comment on lines -302 to -304
guard FileManager.default.fileExists(atPath: file) else {
throw ValidationError("Dockerfile does not exist at path: \(file)")
}
Copy link
Author

Choose a reason for hiding this comment

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

Removing this because the validation is happening in the new checkForDockerOrContainerfile() function. But maybe there is a more clean way to keep all of the validation happening here. Happy to take suggestions on that.

Copy link

Choose a reason for hiding this comment

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

For readability and responsibility determination, I would try to keep validation logic in the validate() function.

Perhaps by moving the defaultBuildFileNames constant from run() to a static attribute, and creating a fun checkBuildFileIsPresent(atPath: String) -> Bool that determines the file is present.

Copy link
Author

Choose a reason for hiding this comment

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

Yeah, I agree. It would be much nicer to keep it here. I'll keep thinking about this one while we debate the merits of just switching the default to Containerfile over on the linked issue:
#99

@bshephar bshephar force-pushed the support-containerfile branch from 0dbb950 to fbc1d96 Compare June 10, 2025 21:31
@bshephar bshephar force-pushed the support-containerfile branch 2 times, most recently from d93e0d6 to 2988b07 Compare June 11, 2025 13:33
@bshephar bshephar marked this pull request as ready for review June 11, 2025 13:37
@bshephar
Copy link
Author

bshephar commented Jun 11, 2025

Ok, this builds and when I test the binary locally, it works to fix the issue I raised:

Testing on:

❯ sw_vers
ProductName:            macOS
ProductVersion:         26.0
BuildVersion:           25A5279m

Implicit Containerfile

❯ ./container build -t fedora:test .
Using Containerfile at: /Users/brendanshephard/Code/okd/fedora-work/Containerfile
[+] Building 5.7s (5/5) FINISHED
 => [resolver] fetching image...docker.io/library/fedora:latest                                                                                                                                                                                         0.0s
 => [internal] load .dockerignore                                                                                                                                                                                                                       0.0s
 => => transferring context: 2B                                                                                                                                                                                                                         0.0s
 => oci-layout://docker.io/library/fedora:latest@sha256:16ca3e34a9545583551589e71882f1f27cbce3c2f1fc6210c932d22a6931b73b                                                                                                                                0.0s
 => => resolve docker.io/library/fedora:latest@sha256:16ca3e34a9545583551589e71882f1f27cbce3c2f1fc6210c932d22a6931b73b                                                                                                                                  0.0s
 => CACHED [linux/arm64 1/2] RUN dnf in pre-commit zsh ripgrep g++ mtr openssh openssh-clients tmux neovim fzf git make gcc go rust nodejs unzip python3-pip -y  && useradd nvim  && useradd -s /usr/bin/zsh -G wheel bshephar                          0.0s
 => exporting to oci image format                                                                                                                                                                                                                       3.3s
 => => exporting layers                                                                                                                                                                                                                                 0.0s
 => => exporting manifest sha256:55e631e502e70ecf8e667b668d144e34951173fa3a2fb2534369e6d1390fe37f                                                                                                                                                       0.0s
 => => exporting config sha256:83a72694cf59b06bf33448a43935332ecca8dd3af17ac28d74ae26fcdca84408                                                                                                                                                         0.0s
 => => exporting manifest list sha256:e5ead77aaf12559ba34defdb6f1d7429431c8febc1daed40d564e78f67bb24a0                                                                                                                                                  0.0s
 => => sending tarball                                                                                                                                                                                                                                  3.3s
Successfully built fedora:test

Bad file:

❯ ./container build -t fedora:test -f blahfile
Error: Provided container build file does not exist: blahfile

Explicit Containerfile:

❯ ./container build -t fedora:test -f Containerfile
Using Containerfile at: /Users/brendanshephard/Code/okd/fedora-work/Containerfile
[+] Building 5.6s (5/5) FINISHED
 => [resolver] fetching image...docker.io/library/fedora:latest                                                                                                                                                                                         0.0s
 => [internal] load .dockerignore                                                                                                                                                                                                                       0.0s
 => => transferring context: 2B                                                                                                                                                                                                                         0.0s
 => oci-layout://docker.io/library/fedora:latest@sha256:16ca3e34a9545583551589e71882f1f27cbce3c2f1fc6210c932d22a6931b73b                                                                                                                                0.0s
 => => resolve docker.io/library/fedora:latest@sha256:16ca3e34a9545583551589e71882f1f27cbce3c2f1fc6210c932d22a6931b73b                                                                                                                                  0.0s
 => CACHED [linux/arm64 1/2] RUN dnf in pre-commit zsh ripgrep g++ mtr openssh openssh-clients tmux neovim fzf git make gcc go rust nodejs unzip python3-pip -y  && useradd nvim  && useradd -s /usr/bin/zsh -G wheel bshephar                          0.0s
 => exporting to oci image format                                                                                                                                                                                                                       3.3s
 => => exporting layers                                                                                                                                                                                                                                 0.0s
 => => exporting manifest sha256:55e631e502e70ecf8e667b668d144e34951173fa3a2fb2534369e6d1390fe37f                                                                                                                                                       0.0s
 => => exporting config sha256:83a72694cf59b06bf33448a43935332ecca8dd3af17ac28d74ae26fcdca84408                                                                                                                                                         0.0s
 => => exporting manifest list sha256:e5ead77aaf12559ba34defdb6f1d7429431c8febc1daed40d564e78f67bb24a0                                                                                                                                                  0.0s
 => => sending tarball                                                                                                                                                                                                                                  3.3s
Successfully built fedora:test

Explicit Dockerfile:

❯ ./container build -t fedora:test -f Dockerfile
Using Dockerfile at: /Users/brendanshephard/Code/okd/fedora-work/Dockerfile
[+] Building 6.0s (5/5) FINISHED
 => [resolver] fetching image...docker.io/library/fedora:latest                                                                                                                                                                                         0.0s
 => [internal] load .dockerignore                                                                                                                                                                                                                       0.0s
 => => transferring context: 2B                                                                                                                                                                                                                         0.0s
 => oci-layout://docker.io/library/fedora:latest@sha256:16ca3e34a9545583551589e71882f1f27cbce3c2f1fc6210c932d22a6931b73b                                                                                                                                0.0s
 => => resolve docker.io/library/fedora:latest@sha256:16ca3e34a9545583551589e71882f1f27cbce3c2f1fc6210c932d22a6931b73b                                                                                                                                  0.0s
 => CACHED [linux/arm64 1/2] RUN dnf in pre-commit zsh ripgrep g++ mtr openssh openssh-clients tmux neovim fzf git make gcc go rust nodejs unzip python3-pip -y  && useradd nvim  && useradd -s /usr/bin/zsh -G wheel bshephar                          0.0s
 => exporting to oci image format                                                                                                                                                                                                                       3.8s
 => => exporting layers                                                                                                                                                                                                                                 0.0s
 => => exporting manifest sha256:55e631e502e70ecf8e667b668d144e34951173fa3a2fb2534369e6d1390fe37f                                                                                                                                                       0.0s
 => => exporting config sha256:83a72694cf59b06bf33448a43935332ecca8dd3af17ac28d74ae26fcdca84408                                                                                                                                                         0.0s
 => => exporting manifest list sha256:e5ead77aaf12559ba34defdb6f1d7429431c8febc1daed40d564e78f67bb24a0                                                                                                                                                  0.0s
 => => sending tarball                                                                                                                                                                                                                                  3.8s
Successfully built fedora:test

Implicit Dockerfile:

❯ ./container build -t fedora:test
Using Dockerfile at: /Users/brendanshephard/Code/okd/fedora-work/Dockerfile
[+] Building 5.3s (5/5) FINISHED
 => [resolver] fetching image...docker.io/library/fedora:latest                                                                                                                                                                                         0.0s
 => [internal] load .dockerignore                                                                                                                                                                                                                       0.0s
 => => transferring context: 2B                                                                                                                                                                                                                         0.0s
 => oci-layout://docker.io/library/fedora:latest@sha256:16ca3e34a9545583551589e71882f1f27cbce3c2f1fc6210c932d22a6931b73b                                                                                                                                0.0s
 => => resolve docker.io/library/fedora:latest@sha256:16ca3e34a9545583551589e71882f1f27cbce3c2f1fc6210c932d22a6931b73b                                                                                                                                  0.0s
 => CACHED [linux/arm64 1/2] RUN dnf in pre-commit zsh ripgrep g++ mtr openssh openssh-clients tmux neovim fzf git make gcc go rust nodejs unzip python3-pip -y  && useradd nvim  && useradd -s /usr/bin/zsh -G wheel bshephar                          0.0s
 => exporting to oci image format                                                                                                                                                                                                                       3.2s
 => => exporting layers                                                                                                                                                                                                                                 0.0s
 => => exporting manifest sha256:55e631e502e70ecf8e667b668d144e34951173fa3a2fb2534369e6d1390fe37f                                                                                                                                                       0.0s
 => => exporting config sha256:83a72694cf59b06bf33448a43935332ecca8dd3af17ac28d74ae26fcdca84408                                                                                                                                                         0.0s
 => => exporting manifest list sha256:e5ead77aaf12559ba34defdb6f1d7429431c8febc1daed40d564e78f67bb24a0                                                                                                                                                  0.0s
 => => sending tarball                                                                                                                                                                                                                                  3.2s
Successfully built fedora:test

@bshephar bshephar mentioned this pull request Jun 11, 2025
@katiewasnothere katiewasnothere requested a review from wlan0 June 11, 2025 16:39
Copy link

@amatzen amatzen left a comment

Choose a reason for hiding this comment

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

I agree with your point that Container CLI should have support for Containerfiles.

I've made some code-related suggestions, but I do think the CLI should default to generic terminology (e.g. Containerfile) and use vendor-specific Dockerfile as a synonym – not vice-versa.

Comment on lines -302 to -304
guard FileManager.default.fileExists(atPath: file) else {
throw ValidationError("Dockerfile does not exist at path: \(file)")
}
Copy link

Choose a reason for hiding this comment

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

For readability and responsibility determination, I would try to keep validation logic in the validate() function.

Perhaps by moving the defaultBuildFileNames constant from run() to a static attribute, and creating a fun checkBuildFileIsPresent(atPath: String) -> Bool that determines the file is present.

@wlan0
Copy link
Contributor

wlan0 commented Jun 15, 2025

Thanks for submitting this PR!

Before proceeding with code review, let’s first discuss and align on the proposed direction. Please contribute your thoughts to the discussion here: #99

Once we’ve reached consensus there, we’ll revisit this PR. Appreciate your cooperation!

@bshephar
Copy link
Author

I agree with your point that Container CLI should have support for Containerfiles.

I've made some code-related suggestions, but I do think the CLI should default to generic terminology (e.g. Containerfile) and use vendor-specific Dockerfile as a synonym – not vice-versa.

Yeah, I think before we push too much further into the PR, it would be nice to understand if the maintainers are open to using Containerfile as the default. If that's the case, we can vastly simplify all of this and just change the default:
https://github.com/apple/container/blob/main/Sources/CLI/BuildCommand.swift#L57

But yeah, if not, I think it would be great to support both at least. Since Dockerfile is very specific to the company "Docker".

This change adds a function to check for either a Dockerfile or
Containerfile. If either exist, and the user hasn't explicitly
provided the file to use, then we can default to the existing file.

Fixes: 99
Signed-off-by: Brendan Shephard <bshephar@bne-home.net>
@bshephar bshephar force-pushed the support-containerfile branch from baba2c0 to e901332 Compare June 16, 2025 09:36
Co-authored-by: Alexander Matzen <alexander@alexander.dk>
@bshephar bshephar force-pushed the support-containerfile branch from e901332 to 9bcfbf7 Compare June 16, 2025 09:37
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 Containerfile
4 participants