>_ IzzyLab

Hands-on task

This task is step 10 of the course “Containers from the inside: namespaces and cgroups”

Docker from the inside: where a container's limits live

medium~12 min

The team is arguing: does docker run --memory really limit anything, or is it just a note in the container's metadata? Check it at the kernel level.

  1. Start a container api from the busybox:1.36 image with the command sleep infinity, a memory limit of 128 MiB (128m) and half a core of CPU.
  2. Find the cgroup directory on the server where Docker put the container, and write its full path (starting with /sys/fs/cgroup) to /root/api-cgroup.txt.
  3. Find the number of the container's network namespace — as lsns shows it, for example 4026532301 — and write it to /root/api-netns.txt.

Look into the limit files of the group you found: what's written there settles the argument.

What you'll practise:
  • docker
  • cgroups
  • namespaces
  • lsns

Topic breakdown

Docker doesn't invent isolation — it assembles it from kernel mechanisms. For every container it creates a set of namespaces (PID, mount, network, UTS, IPC) and a cgroup, into which it puts the container's main process. The docker run flags turn into files of that group: --memory 128m into memory.max (134217728 bytes), --cpus 0.5 into cpu.max (50000 100000). In the end it's the kernel that enforces the limits; Docker just writes numbers into files. Finding a container's group is easy: docker inspect gives the PID of its process on the host, and /proc/<PID>/cgroup gives the group's path. A namespace's number is its inode number, which you see in readlink /proc/<PID>/ns/net and in the first column of lsns.

What's checked

  1. The api container is running
  2. The kernel limits api to 128 MiB of memory
  3. The kernel gives api half a core
  4. /root/api-cgroup.txt holds the container's cgroup directory
  5. /root/api-netns.txt holds the number of api's network namespace

What it looks like