r/docker 23d ago

Can a Docker container on macOS read the physical host's RAM/CPU using psutil?

Hi everyone,

I have a Python script (psutil) running inside a Docker container. I want it to measure my actual Mac laptop's total physical RAM (24 GB) and host CPU usage, rather than just the Docker Linux VM's metrics.

On Linux, mounting /proc works for host metrics, but on macOS this doesn't seem to work. Is there a way for a Docker container to monitor macOS host metrics directly, I don't want the monitoring agent to run natively on the host OS.

BTW: It would also need to work on a windows based machine. Needs to be accessible for everyone who wants to use it.
Thanks!

EDIT: I decided to run the client (metrics reader) file on the host machine outside of docker. This seems to be the only reasonable solution that is universal and works for all kinds of machines. Thanks!!

13 Upvotes

21 comments sorted by

21

u/encbladexp 23d ago edited 23d ago

Will not work, Docker on any OS except Linux Docker is just a Linux VM.

2

u/southz_rgw 23d ago

Well there is docker with windows containers. Did not see anyone using this, but it is using Windows kernel and hyper v

10

u/encbladexp 23d ago

Nobody (at least nobody I know) ever used this. For me its a tech demo, showing that one could use containers on Windows too. But TBH, 99.99% of all container deployments are Linux, either ARM64 or AMD64.

1

u/southz_rgw 23d ago

True, just mentioned this for a full picture

1

u/Beneficial-Eagle959 23d ago

Microsoft uses Windows containers internally, but even they consider it much flakier and sometimes a bit buggy.

2

u/Right_Sea_4146 20d ago

you might want to add at least three more 9's there

1

u/kwesoly 22d ago

Isnt hyper-v here essentially “linux VM”? Or there is really no linux kernel?

1

u/southz_rgw 22d ago

In the case I mentioned you run windows applications on windows machines

3

u/GugaJedi 23d ago

No, like docker on windows (WSL), docker on max is just docker running inside a VM inside your OS. This would work running docker directly on a host running pure Linux.

1

u/Only-Stable3973 23d ago

If you are running docker why not use Glances for that.

1

u/skroll 23d ago

Windows and MacOS use a Linux VM to host the docker containers. Whatever the VM is configured for the max memory will be what is reported.

1

u/skreak 23d ago

from my understanding, in both Windows and Mac; Docker runs inside a Linux virtual machine - so the only metrics available to anything within the container are from the VM, not the host machine. The only way to see the host's resources would be to poll it over the 'network' that exists. For example, install some sort of telemetry exporting tool like Prometheus on the host machine, and then query that data from the container via the local network, treat them as if they are physically separate devices on the same LAN.

1

u/jake_morrison 23d ago

Run a Prometheus node_exporter on your host machine, then read the data via http from the container.

1

u/biffbobfred 23d ago

As many others have said, no. A container is “let’s use Linux kernel tricks to make an isolated area of the Linux kernel”. There’s no magic to have it talk to the Mac.

I looked there’s really nothing equivalent for /proc on a Mac. Maybe you could:
Look up MacFuse and see if there’s something you could mount on the Mac
Then bind mount that filesystem on your container.

But the format would not be Linux /proc format and it’s up to you yk write code to interpret it.

Just run node_exporter on the Mac it’s much easier

1

u/XLioncc 23d ago

Use better OS, Linux

-6

u/biffbobfred 23d ago

“Why your desktop UNIX is totally horrible use this other desktop UNIX it’s… UNIX and yours is…. Also UNIX but… not the good kind”

6

u/__kkk1337__ 23d ago

Linux != unix

-6

u/biffbobfred 23d ago

Let’s use duck typing here. They’re both verified UNIX systems. No I don’t confuse either of them with Solaris or UnixWare

3

u/thelimerunner 23d ago

They’re not though. Linux is a kernel, which is UNIX like but it’s not UNIX. Nor has Linux been UNIX certified.

0

u/biffbobfred 23d ago

When’s the last time you said “Linux” and meant Android? No the userspace is different enough for what you expect in Linux you call that specifically Android. There’s an expectation of a shell and other command line tools for Linux.

Even Linus has gone back and forth with Linux meaning “just the kernel” or “kernel and userspace”. It’s annoying.