To set memory usage limits in Docker while creating and executing the container, the user can utilize “–memory”, and “–memory-reservation” options. In order to set CPU usage limits, utilize “–cpus” and “–cpu-shares” options in the “docker run” or “docker create” commands.

What are Memory and CPU Limits in Docker?

In Docker, containers can directly access and use the system resources according to their need. But sometimes misbehaving containers may use more system resources “Memory and CPU” more than the requirements. In such a situation, the system performance will go down and users may face problems like system overload, slow speed, and many more.

To improve the system performance and to prevent the container from using extra system resources, users may be required to limit the usage of memory and CUP in Docker. 

Let’s first understand the default usage of memory and CPU by executing the container without limiting the system resources. For this purpose, go through the following instructions:

Step 1: Check Default Allocated System Resources

To check the default allocated memory and CPU sources to Docker containers, utilize the “docker info” command. In the below command, the “grep” Linux command will filter out the “CPUs” and “Memory” values:

docker info | grep -iE "CPUs|Memory"
docker info | grep -iE "CPUs|Memory"

Step 2: Run Container Without Limiting the Resources

Now, execute the Docker container with default allocated system resources through the “docker run” command:

docker run --name demo-cont -d html-image

For demonstration, we have created and executed the “demo-cont” through our own “html-image” docker image:

docker run --name demo-cont -d html-image

Now, list down the containers and check if the “demo-cont” is executed or not:

docker ps -a
docker ps -a

Step 3: Check Stats

To check the resource usage stats of the Docker container, execute the “docker stats <cont-name>” command:

docker stats demo-cont

Here, you can see the memory limit is the default set limit which is “1.877GB”. There are 2 processors allocated but currently, the CPU usage percentage is “0%”:

docker stats demo-cont

Now to set the memory and CPU usage according to the user requirements and preferences, follow the below sections.

How to Set Memory Usage Limit in Docker?

Setting memory and other system resource limits can prevent resource starvation for other Docker containers as it stops them from using too much system memory and CPU resources. The user can set two types of memory usage limits for the Docker container:

  • Hard Memory Limit: When a hard memory limit is exceeded by any Docker container, the Docker takes aggressive actions to prevent the memory from over usage such as unschedule or terminate the running container. This will prevent the system from instability and increase the system’s performance.
  • Soft Memory Limit: The soft memory limit is used to set the memory limit to warn the system or container that they are about to exceed the memory usage. It will not take aggressive action but warns the user. The soft memory limit is used by an administrator for monitoring or setting up alerts.

Set Hard Memory Limit

To set up the hard memory limit for the Docker container, the user can utilize the “–memory” option while creating the container through “docker create” or creating and executing the container through the “docker run” command. For a practical demonstration, check out the following procedure:

Step 1: Set Hard Memory Limit 

Create and fire up the container along with limiting the memory resources through the “docker run –memory=<amount of hard memory limit> <image-name>” command:

docker run -d --name demo-cont1 --memory="512m" html-image

In the above command, the “-d” option is utilized to run the container in detached mode, “–name” will set the container name, and “–memory” is used to set the hard memory limit:

docker run -d --name demo-cont1 --memory="512m" html-image

Step 2: Check Docker Container Stats

After that, check the stats of running the Docker container for verification. To this purpose, execute the “docker stats <container-name>” command:

docker stats demo-cont1

The below output shows that we have effectively set the hard memory limit “512MiB” for “demo-cont1”:

docker stats demo-cont1

Bonus Tip: How to Set Memory Swap to Transfer Data to Disk

The “–memory-swap” option is used to set the amount of data that can be swapped to the system disk from memory. The amount of memory swap is always greater than the hard memory limit. This value is the addition of “hard-memory limit + swap memory”. To set the memory swap limit, follow the given demonstration:

Step 1: Set Hard Memory and Swap Memory Limit

To set the hard memory and swap memory while creating and executing the container, utilize “docker run –name <cont-name> –memory=<“memory-limit”> –memory-swap=<“memory-limit + swap-memory-limit”> <image-name>” command:

docker run -d --name demo-cont1 --memory="512m"--memory-swap="1g" html-image
docker run -d --name demo-cont1 --memory="512m"--memory-swap="1g" html-image

Step 2: Inspect Docker Container

Now, inspect the Docker container to check if we have set the memory limit. For this purpose, use the “docker inspect <container-name>” command. Here “grep” command is used to filter the output for the “Memory” keyword only:

docker inspect demo-cont1 | grep Memory

This displayed result shows that we have successfully set the hard memory and swap memory limit in Docker:

docker inspect demo-cont1 | grep Memory

Set Soft Memory Limit

The soft memory limit only alerts the user that the container is using more memory and this may cause the memory starvation condition for other containers and halt the system performance. To set the soft memory limit, the “–memory-reservation” option is used in the “docker run” and “docker create” command. To set the soft memory limit while running the container, look at the below illustrations.

Step 1: Set Soft Memory Limit

To set the soft memory limit, utilize the “–memory-reservation” option in the “docker run” command as done below:

docker run -d --name demo-cont2 --memory-reservation="512m" html-image
docker run -d --name demo-cont2 --memory-reservation="512m" html-image

Step 2: Inspect the Container

For verification, check the container stats through the “docker stats <cont-name>” command:

docker stats demo-cont2

The below output does not show any information about the soft memory limit but displays the default hard memory limit:

docker stats demo-cont2

To view the information about soft memory, inspect the container through the “docker inspect <cont-name>” command:

docker inspect demo-cont2 | grep Memory

In the above command, the “grep” will filter the result for the “Memory” word. From the below output, you can see the “MemoryReservation” value is effectively set which means we have set the soft memory limit for the container:

docker inspect demo-cont2 | grep Memory

In the above output, the hard memory and memory swap values are zero which means there is no hard memory limit:

How to Set the Hard and Soft Memory Limit For Docker Container

By setting up both hard and soft memory limits, the user will first get the alert or warning when exceeding the soft memory limit. But if the memory limit still exceeds the hard memory limit, the container will be terminated or unscheduled. This is an effective way to manage the memory usage for the Docker container.

To set the hard and soft memory limit for the Docker container, go through the below command:

docker run -d --name demo-cont2 --memory="1024m" --memory-reservation="512m" html-image
docker run -d --name demo-cont2 --memory="1024m" --memory-reservation="512m" html-image

For confirmation, again inspect the container through the following command:

docker inspect demo-cont2 | grep Memory

Here, you can see we have set the container’s hard and soft memory usage limit:

docker inspect demo-cont2 | grep Memory

Note: From the above result, you can note that by setting up a hard memory limit, the amount of memory swap is automatically set and it is the addition of “hard-memory limit + swap-memory”. But in case, if the user does not want to swap the data from memory to disk, manually set the memory swap amount. 

How to Avoid Setting Up Swap Memory For Docker Containers?

To avoid setting up the amount of memory swap for the Docker container, the user needs to set the “–memory-swap” value equal to the “hard memory limit”. As in the first section, we have explained the swap memory is “2 * hard-memory-limit”. For illustration, go through the below command:

docker run -d --name demo-cont2 --memory="1024m" --memory-reservation="512m" --memory-swap="1024m" html-image
docker run -d --name demo-cont2 --memory="1024m" --memory-reservation="512m" --memory-swap="1024m" html-image

For confirmation, again inspect the Docker container:

docker inspect demo-cont2 | grep Memory
docker inspect demo-cont2 | grep Memory

That is all about setting up a memory usage limit in Docker. To set up the CPU usage limit, move forward toward the below section.

How to Set CPU Usage Limit in Docker?

By default, there is no limit on CPU usage for containers. However, if containers directly and excessively use the system processors, it will critically affect the system performance. To set the CPU usage limit, the user can use the “–cpus” and “–cpu-shares” options in the “docker run” and “docker create” commands.

Limit CPU Cores

To set the cpus cores limit for the Docker container, utilize the “docker run –name <cont-name> –cpus=<no of cores> <image-name>” command:

docker run -d --name demo-cont3 --cpus=2 html-image

Now, we have effectively limited the container to use at most two CPU processors-cores:

docker run -d --name demo-cont3 --cpus=2 html-image

For verification, inspect the container. In the below command, the “grep” will filter out the output for the “NanoCpus” value:

docker inspect demo-cont3 | grep NanoCpus
docker inspect demo-cont3 | grep NanoCpus

Limit CPU Share Cycle

When the system CPU resources are limited, the user can set the CPU share cycle for the container. For this purpose, the user can use the “–cpu-shares” option:

docker run -d --name demo-cont3 --cpus=2 --cpu-shares=2000 html-image
docker run -d --name demo-cont3 --cpus=2 --cpu-shares=2000 html-image

For confirmation, inspect the container and check the “CpuShares” cycle:

docker inspect demo-cont3 | grep CpuShares
docker inspect demo-cont3 | grep CpuShares

This is all about setting up CPU usage limits in Docker.

Bonus Tip: How to Set Memory and CPU Usage Limits in Docker Compose?

The Docker compose is a Docker plugin that is used to execute the multi-container application or service in separate containers. To set the memory and CPU usage limits in Docker Compose, check out the given illustrations.

Step 1: Create Docker Compose File

First, create a Yaml compose file named “docker-compose.yaml” in nano text editor:

sudo nano docker-compose.yml
sudo nano docker-compose.yml

Add the compose instructions, such as “image”, “ports” and many more. For a demonstration, check out the below snippet:

version: "3"
services:
  html:
    image: html-image
    ports:
      - 8080:80
    mem_limit: 512m
    mem_reservation: 128M
    cpus: 1

In the above snippet:

  • We have created the “html” service to execute the HTML app.
  • The “image” key is utilized to specify the container image.
  • The “ports” key is utilized to define the exposed port for the container.
  • The “mem_limit” key will set the hard memory limit for the “html” service.
  • The “mem_reservation” key will set the soft memory limit.
  • The “cpus” key will set the CPU cores for the “html” service container:
Output

To save the file, press the “CTRL+S” key. Now, exit the editor by pressing the “CTRL+X” key.

Step 2: Up the Compose Service

To execute the composing service in the Docker container through the “docker compose up” command:

docker compose up -d
docker compose up -d

Step 3: Check the Docker Stats

For confirmation, check the Docker stats through the “docker stats” command:

docker stats

In the output below, you can see the hard memory limit is set at “512MiB”. More information about soft memory and CPU cores can be viewed by inspecting the Docker container:

docker stats

We have covered the techniques to set up the memory and CPU usage limit for Docker containers.

Conclusion

To set up memory usage limits for Docker containers, the user can set the hard and soft memory limits through the “–memory” and “–memory-reservation” options respectively in the “docker run” and “docker create” commands. Users can also set the memory swap amount to move the data from memory to disk through the “–memory-swap” option. In order to set the CPU usage limit, utilize the “–cpus” option in the “docker run” or “docker create” command. To set or limit the CPU share cycle, use the “–cpu-shares” option. Users can also set the memory and CPU usage limit in Docker Compose through the “mem_limit”, “mem_reservation”, and “cpus” keys. This write-up has discussed the techniques to limit the memory and CPU usage limit for the Docker containers.