Threat Picture
Latest Cybersecurity News
How to Limit Docker Container Resources (4 Methods)

How to Limit Docker Container Resources (4 Methods)

One runaway container can starve everything else on the host. Four flags that keep CPU and memory on a leash, with the commands to copy.
By Charles Joseph · Updated
Share
Share
Copy URL

You can limit resource usage in Docker containers by specifying constraints on CPU, memory, and other resources when starting a container.

Here are some examples of how to set resource limits for Docker containers:

4 Resource Limits for Docker Containers

Sponsored

Limit CPU Usage

Use the –cpus flag to specify the number of CPUs allocated to the container.

docker run --cpus="1" imagename

In this example, the container is limited to using 1 CPU.

Limit Memory Usage

Use the -m or –memory flag to set the maximum amount of memory a container can use.

docker run -m 512m imagename

In this example, the container is limited to using 512 MB of memory.

Limit the Container’s CPU Shares

Use the –cpu-shares flag to set a relative share of CPU time for the container.

The default value is 1024, and the value you set is relative to the total CPU shares of all containers on the system.

docker run --cpu-shares=512 imagename

In this example, the container is assigned 512 CPU shares.

If there are two containers running — one with 512 CPU shares and one with 1024 CPU shares, the first container would get half the CPU time compared to the second container.

Sponsored

Limit the Container’s CPU Set

Use the –cpuset-cpus flag to specify which CPUs the container is allowed to use.

docker run --cpuset-cpus="0-1" imagename

In this example, the container is limited to using only the first two CPUs (CPU 0 and CPU 1) of the host system.

Summary

These are just a few examples of how you can limit resource usage in Docker containers.

You can find more options and detailed information in the Docker documentation:

Related Posts:

QUOTE:

"Amateurs hack systems, professionals hack people."