Sobes.tech
Junior — Middle

How to distinguish physical resources of the host system from resources allocated to a container in Docker?

sobes.tech AI

Answer from AI

Docker containers run on top of the host system's kernel, utilizing its physical resources (CPU, memory, disk, network). However, containers can have resource limits set at startup.

To distinguish between the host's physical resources and the resources allocated to a container, it is important to understand that:

  • Host physical resources are the actual hardware characteristics: number of CPU cores, total RAM, disk space.
  • Container resources are the limits imposed on the container via Docker parameters, for example:
    • --cpus or --cpu-quota for CPU limits
    • --memory for RAM limits

For example: if the host has 16 GB of RAM and a container is started with --memory=2g, then the container has a maximum of 2 GB of memory available, even though physically 16 GB are available.

To check the container's resources, you can use the command:

docker stats <container_id>

This will show the current CPU and memory usage of the container.

Inside the container, you can also view limits via cgroups, for example:

cat /sys/fs/cgroup/memory/memory.limit_in_bytes

Thus, physical resources are the host's capabilities, while container resources are the limited quotas Docker applies for isolation and management.