By default, Docker containers have access to the full RAM and CPU resources of the host. Leaving them to run with these default settings may lead to performance bottlenecks.
If you don’t limit Docker’s memory and CPU usage, Docker can use all the systems resources.
In this tutorial, learn how to limit memory and CPU usage of Docker containers.
Configure System to Enable Limiting Resources
Before you can run a container with limited resources, check whether your system supports this Docker option. This guide shows you how to do so in Ubuntu, as well as how to enable this feature if needed.
1. First, run the command:
sudo docker info
If you receive the output
WARNING: No swap limit support, limiting resources has not been enabled by default.
2. To add this option, edit the grub configuration file. Open the file in a text editor of your choice (we are using nano):
sudo nano /etc/default/grub
3. Then, add the following line:
4. Save the changes and exit the file.
5. Then, update the grub configuration with the command:
6. Finally, reboot your machine for the changes to take place.
7. To verify you have successfully enabled the given Docker feature by rerunning the
docker info command.
Note: If you still haven’t mastered Docker commands, this Docker Commands Cheat Sheet may be useful as a handy reference sheet.
Limit Docker Container Memory Access
There are several RAM limitations you can set for a Docker container. Some of them include:
- Configuring the maximum amount of memory a container can use.
- Defining the amount of memory a Docker container can swap to disk.
- Setting the soft limit for the amount of memory assigned to a container.
Below, find out how to configure Docker memory limitations.
Set Maximum Memory Access
To limit the maximum amount of memory usage for a container, add the
--memory option to the
docker run command. Alternatively, you can use the shortcut
Within the command, specify how much memory you want to dedicate to that specific container.
The command should follow the syntax:
sudo docker run -it --memory=”[memory_limit]” [docker_image]
The value of
memory_limit should be a positive integer followed by the suffix b, k, m, or g (short for bytes, kilobytes, megabytes, or gigabytes). For example, to limit the container with 1 GB of RAM, add
For example, to run an instance of an Ubuntu container and set the memory limit to 1 GB, the command is:
sudo docker run -it --memory=”1g” ubuntu
Set Swap to Disk Memory Limit
swap option allows you to store data even after all RAM assigned to the container has been used up. It does this by ignoring the memory limitation and writing directly to the disk. Although this is a useful feature, it is not a recommended practice as it slows down performance.
To configure this additional RAM space, define the total amount of swap memory. Before doing this, you should already have the maximum memory (
--memory) of the non-swap memory set. The swap includes the total amount of non-swap memory plus the amount of swap memory reserved as backup.
For example, if you set
--memory to 1 GB, as in the example above, the amount of swap memory needs to be more than that. To run a container with an additional 1 GB of swap memory, set the swap memory to 2 GB.
The syntax for running a container with limited memory and additional swap memory is:
sudo docker run -it --memory=”[memory_limit]” --memory-swap=”[memory_limit]” [docker_image]
For instance, to run a container from the Ubuntu image, assigning 1 GB of RAM for the container to use and reserving 1 GB of RAM for swap memory, type:
sudo docker run -it --memory=”1g” --memory-swap=”2g” ubuntu
Note: If you don’t want to use swap memory, give
--memory-swap the same values.
Set Soft Limit to Container Memory
Limiting the memory usage of a container with
--memory is essentially setting a hard limit that cannot be surpassed. Alternatively, you can set a soft limit (
--memory-reservation) which warns when the container reaches the end of its assigned memory but doesn’t stop any of its services.
--memory limitations see are not set, setting the soft limit with
--memory-reservation doesn’t completely limit container space. If you have both features enabled, the soft limit is always lower than the maximum space capacity.
As an example, for an Ubuntu container to have the memory reservation of 750 MB and the maximum RAM capacity of 1 BG, use the command:
sudo docker run -it --memory=”1g” --memory-reservation=”750m” ubuntu
Limit Docker Container CPU Usage
Just like RAM usage, Docker containers don’t have any default limitations for the host’s CPU. Giving containers unlimited CPU usage can lead to issues.
There are several ways to define how much CPU resources from the host machine you want to assign to containers.
For example, if you have a host with 2 CPUs and want to give a container access to one of them, use the option
--cpus="1.0". The command for running an Ubuntu container with access to 1 CPU would be:
sudo docker run -it --cpus=”1.0” ubuntu
You can also use the
--cpu-shares option to give the container a greater or lesser proportion of CPU cycles. By default, this is set to 1024.
To run a container with lesser CPU shares, run:
sudo docker run -it --cpus-shares=”700” ubuntu
To find more options for limiting container CPU usage, please refer to Docker’s official documentation.
Now you can run Docker containers without having to worry about them stepping on each other and taking up too many resources. For more Docker configuration options, check out How To Use Docker Run Command With Examples.