Feedback

Chat Icon

Painless Docker - 2nd Edition

A Comprehensive Guide to Mastering Docker and its Ecosystem

Managing Container Resources
26%

Memory Usage Reservations and Limits

We are going to see the available options to manage memory usage for a container by following some practical examples. Note that in the following examples, you can use b, k, m, g to indicate bytes, kilobytes, megabytes, or gigabytes (suffixes are case-insensitive).

Setting the Maximum Amount of Memory Available to a Container

docker run -d --memory="512m" nginx

In the above example, we are setting the maximum amount of memory available to the container to 512 MB. If the container exceeds this limit, the kernel may invoke the cgroup out-of-memory (OOM) behavior and kill one or more processes inside the container (often causing the container to stop if PID 1 is killed). Docker enforces a small minimum value for this setting (commonly documented as 6 MB).

Setting the Amount of Memory the Container is Allowed to Swap to Disk

docker run -d  --memory="512m" --memory-swap="1000m" nginx

In the above example, we are setting the amount of memory the container is allowed to swap to disk to 1000 MB. The --memory must be set for the --memory-swap to work.

The configured swap represents the total amount of memory and swap that can be used by the container. If the memory is set to 512 MB and the swap is set to 1000 MB, the container can use 512m from memory and 488m from swap (1000m - 512m).

If --memory-swap is set to the same value as --memory, swap is effectively disabled for the container. If you set it lower than --memory, an error will occur (e.g.: Minimum memoryswap limit should be larger than memory limit)

If you want to use unlimited swap, you can set the --memory-swap to -1.

docker run -d  --memory="512m" --memory-swap="-1" nginx

Setting the Memory Swappiness for a Container

# Lower swappiness value
docker run -d --memory-swappiness="10" nginx

# Higher swappiness value
docker run -d --memory-swappiness=

Painless Docker - 2nd Edition

A Comprehensive Guide to Mastering Docker and its Ecosystem

Enroll now to unlock all content and receive all future updates for free.

Unlock now  $31.99$25.59

Hurry! This limited time offer ends in:

To redeem this offer, copy the coupon code below and apply it at checkout:

Learn More