Okay, you're right! The memory usage shown by the main docker, podman, or orbstack process on your host includes the runtime's overhead and potentially shared resources, not just the memory actively used by the processes inside a specific container.
To see the memory usage from the perspective of the container (which is managed by the kernel using cgroups), here are the most common and effective ways:
1. Using the Container Runtime's stats Command:
This is usually the easiest and most direct way provided by the container runtime itself. It shows real-time or snapshot resource usage per container, pulled from the kernel's cgroup information.
-
For Docker (and usually OrbStack):
docker stats <container_id_or_name>
Or for all running containers:
docker stats
Add --no-stream to get a single snapshot instead of a continuously updating view:
docker stats <container_id_or_name> --no-stream
-
For Podman:
podman stats <container_id_or_name>
Or for all running containers:
podman stats
Add --no-stream for a snapshot:
podman stats <container_id_or_name> --no-stream
Understanding the Output (especially "Memory Usage"):
The "Memory Usage" column in docker stats or podman stats typically shows the total memory currently consumed by the container's cgroup. This often includes:
- RSS (Resident Set Size): The amount of physical RAM being used by the container's processes.
- Page Cache: Memory used by the kernel to cache files the container is accessing. This memory can be reclaimed by the kernel if needed, so it's not always "actively used" by the application processes themselves, but it is memory attributed to the container's cgroup.
The value is usually shown as current_usage / memory_limit. The percentage is (current_usage / memory_limit) * 100.
2. Executing Memory Commands Inside the Container:
You can execute standard Linux memory monitoring commands directly inside the container using docker exec or podman exec. This gives you the memory usage from the perspective of the container's own namespaces.
-
Using free -m (shows system memory usage):
docker exec <container_id_or_name> free -m
# Or for Podman:
podman exec <container_id_or_name> free -m
Look at the used column, particularly the - buffers/cache line if available, or understand that buff/cache is reclaimable.
-
Using ps aux (shows process memory usage):
docker exec <container_id_or_name> ps aux
# Or for Podman:
podman exec <container_id_or_name> ps aux
Look at the %MEM column (percentage of container's memory limit, if set, or total system memory if not limited) and the RSS column (Resident Set Size in kilobytes - actual RAM used by that process). Summing the RSS for all processes inside the container gives you the total RSS for the container.
-
Using top -n 1 (shows process memory usage snapshot):
docker exec <container_id_or_name> top -n 1
# Or for Podman:
podman exec <container_id_or_name> top -n 1
Similar to ps, shows %MEM and RES (Resident memory size, similar to RSS) per process. The summary at the top also shows total memory.
Caveat for Executing Commands: This requires the container image to have the necessary utilities (free, ps, top, bash/sh) installed. Minimal images might not include them.
3. Examining Cgroup Information Directly (Advanced):
Container runtimes use Linux Control Groups (cgroups) to limit and track resource usage. You can inspect the cgroup filesystems directly on the host to see the raw data the kernel maintains.
-
Locate the container's cgroup directory: This varies depending on your OS, cgroup version (v1 or v2), and runtime. Common paths might be under /sys/fs/cgroup/memory/, /sys/fs/cgroup/systemd/, or /sys/fs/cgroup/unified/. Run times like Docker/Podman often put containers under specific paths within these hierarchies (e.g., related to docker, libpod, or crio). You often need the full container ID.
-
Read the usage file: Inside the container's cgroup directory (e.g., .../<container_id>/), look for files like:
memory.usage_in_bytes (cgroup v1 & v2): Total memory usage (including page cache).
memory.stat (cgroup v1): Provides detailed breakdown (rss, cache, mapped_file, etc.).
memory.current (cgroup v2): Similar to memory.usage_in_bytes.
memory.events (cgroup v2): Shows OOM events etc.
-
Example (may vary):
# Find container's cgroup path (complex, might involve systemd-run or runtime internal structure)
# As a simplified example, if you found it:
# cat /sys/fs/cgroup/memory/docker/<full_container_id>/memory.usage_in_bytes
This method is complex and mainly useful for debugging or understanding the underlying mechanism. Stick to docker stats or podman stats or exec for everyday monitoring.
In summary:
- For a quick look at the total memory usage attributed to the container (including cache), use
docker stats or podman stats.
- For a view of memory usage by processes inside the container (RSS), use
docker exec or podman exec with ps aux or top.