Sometimes, you will need to limit the resources of multiple containers at once. For example, you may want to run a local development version of a service that consists of a web frontend, an API backend, and a database. To protect the host system, you want to ensure the entire deployment uses no more than 50% of the host’s CPU and 500 MB of memory, and at the same time, the optimal distribution of resources between the containers is unknown or varies depending on the specific use case.
This is exactly what this challenge is about.
Your task is to start the ~/app
Docker Compose application,
limiting its total CPU and memory usage as follows:
- Individual containers should not have CPU and memory limits set.
- The total CPU usage of the entire application should be limited to 50% of the host’s CPU.
- The total memory usage of the entire application should be limited to 500 MB.
Ensure that all three containers (and only those three) are up and running.
Hint 1 💡
Unlike with namespaces, you cannot start a container in an existing cgroup. However, for some reason, Docker gives you control over the parent cgroup of the container. Can it be helpful for our task?
Hint 2 💡
Cgroups are hierarchical, and child cgroups inherit the limits of their parents.
Hint 3 💡
Docker Compose is based on the docker run
command.
Can you spot a relevant flag in the docker run --help
output?
Hint 4 💡
In Docker Compose, multiple containers can use the same parent cgroup.
Just populate the cgroup_parent
attribute of the api
, web
, and rdb
services in the docker-compose.yml
file with the path to the common parent cgroup.
Hint 5 💡
Configured a custom cgroup, updated the docker-compose.yml
to use it,
but now getting the cgroup-parent for systemd cgroup should be a valid slice named as "xxx.slice"
error while running docker compose up
?
This is because Docker relies on systemd
(when it's available) to manage cgroups,
and so should you.
Hint 6 💡
Not sure how to create a cgroup with systemd
-
check out this step-by-step tutorial Controlling process resources with Linux cgroups.
Level up your Server Side game — Join 7,800 engineers who receive insightful learning materials straight to their inbox