-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
psutil inside docker container reports host stats #1011
Comments
You can change
See:
Please note that most information on Linux is retrieved via /proc but not all of it (e.g. |
Update: |
psutil.PROCFS_PATH = "/something_else" hey, for my container, what would be the '/something_else' ? |
I don't know. I don't have a Docker instance to check against. Try to google for "docker proc filesystem location" to see where it is located. |
@crazyglasses You will have to mount the root mount namespace's |
@keitwb Basically what I want to do is run psutil inside a python script inside a container. A few comments that I have gotten is that by default the psutil is pointed towards the containers /proc and that I would get metrics of the container if I use it that way . What you are suggesting seems to be mounting the root inside the container, doesnt that mean I get the host stats only? |
@crazyglasses Sorry, misread your question. For docker containers, there isn't a hard distinction between CPU/mem inside or outside the container like there would be on a VM -- all containers share the same kernel and there isn't a namespace for cpus or memory that causes the container to see those differently. There are cgroups, which you can use to limit resource usage of the containers (e.g. certain cores or period/quota of CPU), but that would just show as the processes in the container using a more limited amount of resources, not change what psutil would report as far as I understand it. I know |
So |
|
@giampaolo So how can I collect host stats ? thanks |
I don’t know enough about containers but I guess you can mount host’s /proc fs so that it’s visibile from within the container. With that in place you can: ‘’’ From then on most psutil’s API will return stats about the host. All of this is based on the assumption that host and container’s /proc filesystems are independent, of which I am not completely sure. If they are then querying /proc for things such as system wide stats (CPU, memory, ...) is safe, but querying processes is not. |
OK, thanks alot. |
It seems that this is not correct: This blog posts shows that /proc/meminfo shows the information from the host and not the container: https://ops.tips/blog/why-top-inside-container-wrong-memory/ You can get the memory via the proposed way in #490 |
I get different results when running on host and inside a docker container. How can I get the host's stats when running inside a container? |
apologies for resurrecting this, but can I use the proc trick to use psutil.net_io_counters() and get the host machines bytes_sent/bytes_recievd ? |
Yes, you should because psutil.net_io_counters() retrieves its data from /proc/net/dev. |
Yeah I thought the same but it doesn't work, psutil.net_io_counters() only returns the data from the container. There is something weird going on that's above my paygrade. The only way I could get it to work was to start the container using '--net=host', which gets psutil.net_io_counters() retrieving the host network info, but that isn't ideal. |
Also see discussion at #2100. |
If I use psutil inside a docker container, it reports the host machine stats, i.e. CPU and RAM.
I would expect it to grab a container-level statistic.
The text was updated successfully, but these errors were encountered: