Repository navigation
dockerfile: run buildkitd within a cgroup namespace for cgroup v2 #6368
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
Merged
tonistiigi
merged 2 commits into
moby:master
from
marxarelli:review/unshare-cgroupns-entrypoint
Feb 24, 2026
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| #!/bin/sh | ||
| # | ||
| # For cgroup v2, ensure buildkitd has a namespaced view of /sys/fs/cgroup by | ||
| # running in a new cgroup and mount namespace and remounting /sys/fs/cgroup. | ||
| # Assume we are already in our own cgroup ns if the current cgroup path is | ||
| # "/". | ||
| # | ||
| # Note this is a workaround for the lack of cgroupns control in the Kubernetes | ||
| # API. If KEP-5714 is adopted, this can eventually be removed. | ||
| # | ||
| # See https://github.com/kubernetes/enhancements/issues/5714 | ||
|
|
||
| set -e | ||
|
|
||
| if [ -e /sys/fs/cgroup/cgroup.controllers ]; then | ||
| if [ "$(cut -d: -f3 /proc/self/cgroup)" != "/" ]; then | ||
| echo creating cgroup namespace >&2 | ||
| exec /usr/bin/unshare --cgroup --mount /usr/bin/with-cgroupfs-remount /usr/bin/buildkitd "$@" | ||
| fi | ||
| fi | ||
|
|
||
| exec /usr/bin/buildkitd "$@" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| #!/bin/sh | ||
| set -e | ||
|
|
||
| options="$(awk '$2 == "/sys/fs/cgroup" { print $4 }' /proc/self/mounts)" | ||
| umount /sys/fs/cgroup | ||
| mount -t cgroup2 -o "$options" cgroup2 /sys/fs/cgroup | ||
|
|
||
| exec "$@" |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the case of
docker run --privilegedthis script does not seem needed, as Docker unshares cgroupns even for privileged mode.So this entrypoint script should be opt-in.
It should be also marked as a workaround until Kubernetes supports unsharing cgroupns.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
KEP:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ping @marxarelli
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for opening that KEP @AkihiroSuda. I added some words of support in the PR discussion.
I'll work on refactoring this PR to be opt-in as you requested.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The entrypoint should already handle this case heuristically by testing whether the current cgroup path is "/" (which is true when run via
docker run --privilegedunless--cgroupns hostis also given). In that case, it skipsunshareand thewith-cgroupfs-remountwrapper script and just doesexec buildkitd.I could also introduce an additional environment variable if you think that's best, but I personally quite like the heuristic behavior.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just added a comment about it being a workaround.