Limiting the resource usage of a container
One of the great features of a container, apart from encapsulating application processes, is the possibility of limiting the resources a single container can consume at most. This includes CPU and memory consumption. Let’s have a look at how limiting the amount of memory (RAM) works:
$ docker container run --rm -it \ --name stress-test \ --memory 512M \ ubuntu:22.04 /bin/bash
Once inside the container, install the stress
tool, which we will use to simulate memory pressure:
/# apt-get update && apt-get install -y stress
Open another terminal window and execute the docker stats
command to observe the resource consumption of all running Docker containers. You should see something like this:
Figure 8.2 – The Docker stats showing a resource-limited container
Look...