Resource limits

The last command we ran showed us the resource utilization of our containers; by default, a container when launched will be allowed to consume all the available resources on the host machine if it requires it. We can put caps on the resources our containers can consume; let's start by updating the resource allowances of our nginx-test container.

Typically, we would have set the limits when we launched our container using the run command; for example, to halve the CPU priority and set a memory limit of 128M, we would have used the following command:

$ docker container run -d --name nginx-test --cpu-shares 512 --memory 128M -p 8080:80 nginx

However, we didn't need to update our already running container; to do this, we can use the update command. Now you must have thought that this should entail just running the following command:

$ docker container update --cpu-shares 512 --memory 128M nginx-test

Running the preceding command will actually produce an error:

Error response from daemon: Cannot update container e6ac3ce1418d520233a68f71a1e5cb38b1ab0aafab5fa00d6e0220975706b246: Memory limit should be smaller than already set memoryswap limit, update the memoryswap at the same time

So what is the memoryswap limit currently set to? To find this out, we can use the inspect command to display all of the configuration data for our running container; just run the following:

$ docker container inspect nginx-test

As you can see, there is a lot of configuration data. When I ran the command, a 199-line JSON array was returned. Let's use the grep command to filter out just the lines that contain the word memory:

$ docker container inspect nginx-test | grep -i memory

This returns the following configuration data:

 "Memory": 0,
"KernelMemory": 0,
"MemoryReservation": 0,
"MemorySwap": 0,
"MemorySwappiness": -1,

Everything is set to 0: how can 128M be smaller than 0? In the context of the configuration of the resources, 0 is actually the default value and means that there are no limits--notice the lack of M at the end. This means that our update command should actually read the following:

$ docker container update --cpu-shares 512 --memory 128M --memory-swap 256M nginx-test
Paging is a memory-management scheme in which the kernel stores and retrieves, or swaps, data from secondary storage for use in main memory. This allows processes to exceed the size of available physical memory.

By default, when you set --memory as part of the run command, Docker will set the --memory-swap size to be twice of that of --memory.

If you run docker container stats nginx-test now, you should see our limits in place:

Also, rerunning docker container inspect nginx-test | grep -i memory will show the changes:

 "Memory": 134217728,
"KernelMemory": 0,
"MemoryReservation": 0,
"MemorySwap": 268435456,
"MemorySwappiness": -1,
The values when running docker container inspect are all shown in bytes.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset