Skip to main content

Configuring CPU and Memory

Let's imagine you need to run four containers for a new project. These containers have the following characteristics:

  • Two web servers.

  • Two MySQL DB.

Obviously, since these are different services, in most cases they have different resource consumption characteristics, such as CPU and memory.

  • Web server -- 0.5 CPU | 128 MB of memory

  • MySQL DB -- 1 CPU | 256 MB of memory

So now what? :(

By default, when you run a container without specifying the amount of resources it will use, it runs without any control, and can even impact the host where it's being executed.

Therefore, it's very important to limit the resource usage of your containers, aiming for better utilization of your computational resources, as we'll see now. :)

5.1. Specifying the amount of memory​

First, let's run a container for our example.

root@linuxtips:~# docker container run -ti --name teste debian

Now let's view the amount of memory configured for this container. An easy way is to use the output of the "docker container inspect" command:

root@linuxtips:~# docker container inspect teste | grep -i mem
"CpusetMems": "",
"KernelMemory": 0,
"Memory": 0,
"MemoryReservation": 0,
"MemorySwap": 0,
"MemorySwappiness": -1,

As we can see, the values corresponding to memory are zeroed, meaning no limit is set.

Now let's run a new container, but passing the values so it uses 512 MB of memory:

root@linuxtips:~# docker container run -ti -m 512M --name novo_container debian

We use the "-m" parameter to specify the amount of memory we want to make available to the container. Let's use "docker container inspect" again to check if our configuration worked:

root@linuxtips:~# docker container inspect novo_container | grep -i mem
"CpusetMems": "",
"KernelMemory": 0,
"Memory": 536870912,
"MemoryReservation": 0,
"MemorySwap": -1,
"MemorySwappiness": -1,

Very good, it seems to have worked!

We can see in the "Memory" field the value used when creating the container. It's worth noting that the displayed value is in bytes.

When you use the "-m" or "--memory" parameter, you're setting the maximum amount of memory the container will use from the host.

5.2. Specifying the amount of CPU​

To limit the amount of CPUs that containers will consume, just use the "--cpu" parameter. With it, you can specify the amount of CPUs you want to make available to a specific container. For example, if we use "--cpus=0.5" we're telling Docker to limit the container's consumption to half a CPU. Easy, right?

# docker container run --cpus=0.5 --name teste1 nginx

With the previous command, we're running a container using the Nginx image -- nothing new here, right? However, now we pass the "--cpus=0.5" parameter telling Docker that this container can use at most 0.5 CPU, which is half of 1 core. :D Simple as flying.

To verify, let's use the "docker container inspect" command:

root@linuxtips:~# docker container inspect teste1 | grep -i cpu
"CpuShares": 0,
"NanoCpus": 500000000,
"CpuPeriod": 0,
"CpuQuota": 0,
"CpusetCpus": "",
"CpusetMems": "",

The "NanoCpus" field shows the information we configured. :)

Simple, easy, and fast!

5.3. Can I change CPU and memory of my running containers?​

Yes! \o/

With the release of Docker version 1.10, we have the "docker update" command, which allows you to change settings related to CPU, memory, and I/O on a running container very simply! This is fantastic!

As an example, we'll run a container and also change the information related to memory and CPU:

root@linuxtips:~# docker container run -ti --cpus=4 -m 512m --name teste1 nginx

root@linuxtips:~# docker container inspect teste1 | grep -i cpu
"CpuShares": 0,
"NanoCpus": 4000000000,
"CpuPeriod": 0,
"CpuQuota": 0,
"CpusetCpus": "",
"CpusetMems": "",

root@linuxtips:~# docker container inspect teste1 | grep -i mem
"CpusetMems": "",
"KernelMemory": 0,
"Memory": 536870912,
"MemoryReservation": 0,
"MemorySwap": -1,
"MemorySwappiness": -1,

Now, let's change the CPU and memory limit values:

root@linuxtips:~# docker container update -m 256m --cpus=1 teste1
teste1

root@linuxtips:~# docker container inspect teste1 | grep -i cpu
"CpuShares": 0,
"NanoCpus": 1000000000,
"CpuPeriod": 0,
"CpuQuota": 0,
"CpusetCpus": "",
"CpusetMems": "",

root@linuxtips:~# docker container inspect teste1 | grep -i mem
"CpusetMems": "",
"KernelMemory": 0,
"Memory": 268435456,
"MemoryReservation": 0,
"MemorySwap": -1,
"MemorySwappiness": -1,

Did it work? YESSS!

So, with the containers running, we changed the information related to memory and CPU!

There are other parameters for "docker container update". To check the complete list, type "docker update --help".