Skip to content

fix(cli): improve container detection when cgroupns=private #15156

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
merged 1 commit into from
Oct 21, 2024

Conversation

DanielleMaywood
Copy link
Contributor

Fixes #12721

If a container in docker is started with --cgroupns=private (which is the default behaviour in docker) then /proc/1/cgroup has the following content:

0::/

If a container in docker is started with --cgroupns=host then /proc/1/cgroup has the following content (hash will vary):

0::/docker/aa86ac98959eeedeae0ecb6e0c9ddd8ae8b97a9d0fdccccf7ea7a474f4e0bb1f

Currently we are determining if a host is containerized by assuming the second scenario. This means the existing behaviour of sniffing /proc/1/cgroup is not always sufficient for checking if a host is containerized.

According to the cgroups(7) man-page there exists a cgroup.type file in a nonroot cgroup. This exists in Linux versions after 4.14.

Linux 4.14 added thread mode for cgroups v2.

With the addition of thread mode, each nonroot cgroup now contains a new file, cgroup.type

This means we can check for the existence of /sys/fs/cgroup/cgroup.type to see if we are in a container or not.

@DanielleMaywood DanielleMaywood changed the title fix(cli): IsContainerized return true when cgroupns=private fix(cli): make IsContainerized return true when cgroupns=private Oct 21, 2024
@DanielleMaywood DanielleMaywood changed the title fix(cli): make IsContainerized return true when cgroupns=private fix(cli): container detection when cgroupns=private Oct 21, 2024
@DanielleMaywood DanielleMaywood changed the title fix(cli): container detection when cgroupns=private fix(cli): detect when in a container when cgroupns=private Oct 21, 2024
@DanielleMaywood DanielleMaywood changed the title fix(cli): detect when in a container when cgroupns=private fix(cli): detect host is a container when cgroupns=private Oct 21, 2024
@DanielleMaywood DanielleMaywood changed the title fix(cli): detect host is a container when cgroupns=private fix(cli): improve container detection when cgroupns=private Oct 21, 2024
@DanielleMaywood DanielleMaywood marked this pull request as ready for review October 21, 2024 13:05
@DanielleMaywood DanielleMaywood requested review from johnstcn and dannykopping and removed request for dannykopping October 21, 2024 13:11
Copy link
Member

@johnstcn johnstcn left a comment

Choose a reason for hiding this comment

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

👍

@DanielleMaywood DanielleMaywood merged commit 13f6645 into main Oct 21, 2024
45 checks passed
@DanielleMaywood DanielleMaywood deleted the dm-groupns-iscontainerized branch October 21, 2024 14:28
@github-actions github-actions bot locked and limited conversation to collaborators Oct 21, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Failed to detect container environment when using cgroups
2 participants