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 docker with cgroup2 detection #116

Merged
merged 4 commits into from
Apr 2, 2024

Conversation

bongole
Copy link
Contributor

@bongole bongole commented Mar 29, 2024

Issue #, if available:

  • Amazon Linux 2023 runs docker in cgroup2, so EbsSqsActiveJobMiddleware#app_runs_in_docker_container? which only assumes cgroup1 will return false.

Description of changes:

  1. Refactored the code of detecting cgroup1 using external commands and add its tests.
  2. Add the code of detecting cgroup2 and add its tests.

By submitting this pull request, I confirm that my contribution is made under
the terms of the Apache 2.0 license.

Add docker with cgroup2 test
Refactor docker with cgroup1 detection
Add docker with cgroup1 detection test
Copy link
Contributor

@mullermp mullermp left a comment

Choose a reason for hiding this comment

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

I think the change makes sense.

end

def in_docker_containr_with_cgroup2?
File.exist?('/proc/1/mountinfo') && File.read('/proc/1/mountinfo') =~ %r{/docker/containers/}
Copy link
Contributor

Choose a reason for hiding this comment

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

This article suggests /proc/self/mountinfo

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for reviewing.
Fixed in the following commit.
845580b

@@ -85,7 +85,15 @@ def sent_from_docker_host?(request)
end

def app_runs_in_docker_container?
@app_runs_in_docker_container ||= `[ -f /proc/1/cgroup ] && cat /proc/1/cgroup` =~ /docker/
@app_runs_in_docker_container ||= in_docker_containr_with_cgroup1? || in_docker_containr_with_cgroup2?
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there a better way other than cgroups (i.e. dockerenv file)? Based on my searching, it seems to be the best, but all options don't seem great.

I'm open to re-evaluating this.

Copy link
Contributor Author

Copy link
Contributor

@alextwoods alextwoods left a comment

Choose a reason for hiding this comment

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

LGTM! Thanks for the contribution!

@app_runs_in_docker_container ||= in_docker_containr_with_cgroup1? || in_docker_containr_with_cgroup2?
end

def in_docker_containr_with_cgroup1?
Copy link
Contributor

Choose a reason for hiding this comment

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

Not sure if you noticed, but this should be container (you forgot the e)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

oh, it's typo.
thanks.
Fixed in the following commit.
21c62ec

@jterapin
Copy link
Contributor

jterapin commented Apr 2, 2024

Thanks for the PR! I'll merge it and will release a new version of the gem sometime today.

@jterapin jterapin merged commit ac74f03 into aws:main Apr 2, 2024
37 of 39 checks passed
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.

None yet

4 participants