CodeGym /Courses /Docker SELF /Container Resource Management

Container Resource Management

Docker SELF
Level 12 , Lesson 0
Available

6.1 Why Limit Container Resources

Docker is awesome at managing container resources, letting you efficiently allocate system power and avoid unnecessary conflicts between containers. This is especially important to make sure an app in one container doesn’t "hog" all the resources and slow down other services. Limiting resources like CPU and memory helps prevent overloads, keeps the system stable, and ensures all apps run predictably. In this lecture, we'll look at how to set limits on CPU and memory usage so your system stays productive even under high loads.

Why it's important to limit container resources:

  1. stability: prevents one container from taking all the resources, causing other containers or the system itself to slow down.
  2. efficiency: better resource distribution between containers to maximize the productive use of system power.
  3. protection: resource limits help avoid problems with containers that might accidentally or intentionally overload the system.

6.2 CPU Usage Limits

Docker gives you several ways to limit how much CPU containers can use.

Important! Here, CPU means one processor core, not the entire processor. A quad-core processor will have 4 CPU.

1. Limiting CPU Share (--cpu-shares)

The --cpu-shares parameter defines the relative priority value for a container's CPU usage. The default value is 1024. This is a relative value, which means that a container with --cpu-shares=512 will get half the CPU priority compared to a container with --cpu-shares=1024.

Usage Example:

In this example, the high_priority_container will have a higher priority for CPU allocation compared to the low_priority_container.

Terminal

docker run -d --name low_priority_container --cpu-shares=512 nginx

docker run -d --name high_priority_container --cpu-shares=2048 nginx

2. Limiting Number of CPUs (--cpus)

The --cpus parameter sets the exact number of CPU available to the container. For example, a value of 1.5 means the container can use 1.5 CPU.

Usage Example:

This example limits the limited_cpu_container to using no more than 1.5 CPU.

Terminal


        docker run -d --name limited_cpu_container --cpus="1.5" nginx
    

3. Limiting CPU Time Usage (--cpu-quota and --cpu-period)

The --cpu-quota and --cpu-period parameters let you fine-tune CPU usage. --cpu-period sets the time interval in microseconds (default is 100000), and --cpu-quota defines the max allowed CPU time for that period.

Usage Example:

In this example, the custom_cpu_quota_container will use no more than 50% CPU (25000/50000).

Terminal


        docker run -d --name custom_cpu_quota_container --cpu-period=50000 --cpu-quota=25000 nginx

6.3 Memory Usage Limits

Memory limits help control how much RAM your container can use. This prevents situations where a single container might start "eating up" all the memory, impacting other processes and containers.

1. Setting a maximum memory limit (--memory)

The --memory parameter sets an upper limit on the amount of memory a container can use. If the container exceeds this limit, the system will terminate it.

Usage example:

This example limits the container limited_memory_container to using no more than 512 MB of RAM.

Terminal

docker run -d --name limited_memory_container --memory="512m" nginx

2. Limiting memory swap usage (--memory-swap)

The --memory-swap parameter sets the total limit for RAM and swap memory. For example, if --memory is set to 512 MB and --memory-swap to 1 GB, the container can use up to 512 MB of RAM and another 512 MB of swap memory.

Usage example:

This example limits the container swap_limited_container to using 512 MB of RAM and 512 MB of swap memory.

Terminal

docker run -d --name swap_limited_container --memory="512m" --memory-swap="1g" nginx

3. Restricting memory usage without swap (--memory-swap=-1)

To disable swap usage, set the --memory-swap parameter to -1.

Usage example:

This example limits the container no_swap_container to 512 MB of RAM with no swap.

Terminal

docker run -d --name no_swap_container --memory="512m" --memory-swap="-1" nginx

6.4 Practical Scenarios

1. Running High-Priority and Low-Priority Tasks

If both critical and secondary tasks run on the same server, the --cpu-shares parameter helps distribute resources so that critical tasks get more CPU.

Terminal

docker run -d --name high_priority_task --cpu-shares=2048 my_high_priority_image
docker run -d --name low_priority_task --cpu-shares=512 my_low_priority_image

2. Limiting Resources for Testing

To test applications under restricted resource conditions, you can set hard limits on CPU and memory. This helps understand how the app will perform under such constraints.

Terminal

docker run -d --name test_container --cpus="1" --memory="256m" my_test_image

3. Protecting Against Resource Exhaustion

To prevent a situation where one container consumes all the system's available resources, you can set memory and CPU limits for all containers.

Terminal

docker run -d --name isolated_container --cpus="2" --memory="1g" my_app_image
3
Task
Docker SELF, level 12, lesson 0
Locked
Limiting CPU usage for a container
Limiting CPU usage for a container
3
Task
Docker SELF, level 12, lesson 0
Locked
Setting memory limits for a container
Setting memory limits for a container
3
Task
Docker SELF, level 12, lesson 0
Locked
Limit CPU and memory usage of the container
Limit CPU and memory usage of the container
3
Task
Docker SELF, level 12, lesson 0
Locked
Limiting the Swap Memory Size of a Container
Limiting the Swap Memory Size of a Container
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION