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

determine if nbresuse is running in docker or pod #66

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

Conversation

xianglei
Copy link

@xianglei xianglei commented Sep 7, 2020

Returns the correct memory & cpu values when nbresuse running in a docker or pod environment

Get resources and usages in docker, and return the correct values i think
Determine if nbresuse running in docker
@xianglei
Copy link
Author

xianglei commented Sep 7, 2020

forgot import os module

@xianglei xianglei closed this Sep 7, 2020
@xianglei xianglei reopened this Sep 7, 2020
@jtpio
Copy link
Member

jtpio commented Dec 23, 2020

Thanks @xianglei. It's true the limits are for now not accurate when running in a container (not taking the cgroups limits into account).


@staticmethod
def __if_dockerized():
if os.path.exists('/.dockerenv'):
Copy link
Member

Choose a reason for hiding this comment

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

Is this a reliable way of detecting whether it is running in Docker? Does this file exist with other container engines?

Copy link

Choose a reason for hiding this comment

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

It's not present on Podman.
I found this stackoverflow question which provides a bit of background: https://superuser.com/questions/1021834/what-are-dockerenv-and-dockerinit

Copy link

@manics manics Dec 23, 2020

Choose a reason for hiding this comment

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

I just checked with Kubernetes K3S too, it's also not present.

However it looks like /sys/fs/cgroup/memory/memory.limit_in_bytes is set to <very large number> if there's no limit:

k3os-5708 [~]$ kubectl run tmp-shell --rm -i --tty --image busybox -- /bin/sh
If you don't see a command prompt, try pressing enter.
/ # cat /.dockerenv
cat: can't open '/.dockerenv': No such file or directory
/ # cat /sys/fs/cgroup/memory/memory.limit_in_bytes
9223372036854771712
k3os-5708 [~]$ kubectl run tmp-shell --rm -i --tty --image busybox --limits=memory=10Mi -- /bin/sh
If you don't see a command prompt, try pressing enter.
/ # cat /sys/fs/cgroup/memory/memory.limit_in_bytes
10485760

So one solution might be to see if /sys/fs/cgroup/memory/memory.limit_in_bytes exists, and if so use min(/sys/fs/cgroup/memory/memory.limit_in_bytes, total-system-memory), and skip the container check completely?

It's also worth noting that cgroups2 is different, I haven't figured out how to find the memory limit there.

@krassowski
Copy link
Collaborator

It looks like there are conflicts which cannot be resolved via UI, is this PR still relevant?

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.

4 participants