Skip to main content

Configuring Resource Constraints

1. Limiting Memory Usage

Option Values Description
--memory 1g Set memory limit for the container (e.g., 1GB)
--memory-swap 2g Set total memory + swap limit (e.g., 2GB)
--memory-swappiness 60 Control tendency to swap (0-100, default 60)
--memory-reservation 512m Soft limit for memory (e.g., 512MB)
--kernel-memory 512m Limit kernel memory (deprecated in newer versions)

Example: Setting Memory Limits

docker run --memory=1g --memory-swap=2g nginx
Note: Memory limits are enforced at the container level and can be set using --memory and --memory-swap. The swap limit includes both RAM and swap space.

2. Setting Memory Reservation

Option Values Description
--memory-reservation 512m, 1g Memory reservation for the container (soft limit)
--memory 1g, 2048m Memory limit for the container (hard limit)

Example: Setting Memory Reservation

docker run --memory-reservation 512m --memory 1g my-app
Note: Memory reservation is a soft limit, while memory is the hard limit. The container can use more than the reserved amount if available.

3. Configuring Swap Limit

Option Values Description
--memory-swap string Swap limit equal to memory plus swap: '100m' or '2g'. If set to -1, the container will have unlimited swap.
--memory-swap=container string Swap limit equal to memory plus swap: '100m' or '2g'. If set to -1, the container will have unlimited swap.

Example: Setting Swap Limit for a Container

docker run --memory-swap=2g --memory=1g my-app
Note: The --memory-swap flag requires that --memory is also set. If --memory-swap is not specified, the container will use a default swap limit equal to the memory limit.

4. Limiting CPU Usage

Option Values Description
--cpus 0.5 Limits the CPU usage to 50% of a single CPU core
--cpu-period 100000 Specifies the CPU period in microseconds (default is 100000)
--cpu-quota 50000 Specifies the CPU time within the period (default is -1, no limit)
--cpu-shares 1024 Relative weight for CPU allocation (default is 1024)

Example: Limiting CPU Usage to 50% of One Core

docker run --cpus="0.5" ubuntu:latest

Example: Setting CPU Period and Quota

docker run --cpu-period=100000 --cpu-quota=50000 ubuntu:latest

5. Setting CPU Shares

Option Values Description
--cpu-shares 1024 Relative weight of CPU time for the container (default is 1024)
--cpu-quota -1 Limit CPU usage per period (in microseconds)
--cpu-period 100000 Control the CPU period (in microseconds) for quota

Example: Setting CPU Shares for a Container

docker run --cpu-shares 512 my-app

Example: Using CPU Quota and Period

docker run --cpu-quota 50000 --cpu-period 100000 my-app
Note: CPU shares are relative and only effective when multiple containers are competing for CPU resources.

6. Pinning to Specific CPUs

Option Values Description
--cpuset-cpus 0,1,3 Restricts the container to run only on the specified CPUs
--cpuset-cpus 0-3 Restricts the container to run only on CPUs 0 through 3

Example: Running a container on specific CPUs

docker run --cpuset-cpus="0,1" my-app
Note: The --cpuset-cpus option restricts the container to specific CPU cores. This is useful for performance optimization or ensuring containers do not compete for resources on the same core.

7. Setting Block I/O Weight

Option Values Description
--blkio-weight default: 100 Block I/O weight (relative weight) for the container. Valid range is 10-1000.

Example: Setting Block I/O Weight

docker run --blkio-weight 300 my-app
Note: The --blkio-weight option controls the relative I/O bandwidth of containers. It does not guarantee a specific amount of I/O but provides a proportional allocation based on the weight values.

8. Limiting Device Read/Write

Option Values Description
--device-read-bps <device>:<rate> Limit read rate (bytes per second) for a specific device
--device-write-bps <device>:<rate> Limit write rate (bytes per second) for a specific device
--device-read-iops <device>:<iops> Limit read I/O operations per second for a specific device
--device-write-iops <device>:<iops> Limit write I/O operations per second for a specific device

Example: Limiting Device Read/Write

docker run --device-read-bps /dev/sda:100mb \
           --device-write-bps /dev/sda:50mb \
           --device-read-iops /dev/sda:1000 \
           --device-write-iops /dev/sda:500 \
           ubuntu:20.04
Note: These options apply to block devices inside the container. The device path must be accessible within the container's filesystem.

9. Setting PID Limits

Option Values Description
--pid host or container[:pid] Controls PID namespace sharing. host shares the host's PID namespace; container creates a new one.
--pids-limit -1 or <number> Sets the maximum number of processes allowed in the container. -1 means no limit.

Example: Setting PID Limits

docker run --pids-limit 100 my-app
Note: The --pids-limit option is supported in Docker 20.10 and later.

10. Configuring Restart Policies

Policy Description Usage Example
no Never restart the container if it stops or crashes. "restart": "no"
on-failure[:max-retries] Restart the container if it exits with a non-zero exit code. Optionally specify max retries. "restart": "on-failure:5"
always Always restart the container regardless of exit status. "restart": "always"
unless-stopped Restart the container unless it was stopped by the user. "restart": "unless-stopped"

Example: Using restart policy in Docker Compose

services:
  web:
    image: nginx
    restart: unless-stopped

Example: Using restart policy with Docker CLI

docker run --restart=on-failure:3 my-app

11. Setting Stop Timeout

Option Values Description
--stop-timeout default: 10 Number of seconds to wait before killing a container during stop operation

Example: Setting Stop Timeout

docker run --stop-timeout 30 my-app
Note: The stop timeout is the duration Docker waits for a container to shut down gracefully before forcibly terminating it. This setting is crucial for ensuring proper cleanup of resources and avoiding data loss.

12. Configuring OOM Killer Behavior

Option Values Description
--oom-kill-disable false Disables the OOM killer for the container. NEW in Docker 24.0
--oom-score-adj -1000 to 1000 Adjusts the OOM score of the container process. Lower values make it less likely to be killed.

Example: Setting OOM Score Adjustment

docker run --oom-score-adj=-500 my-app
Note: The OOM killer behavior can be fine-tuned using /sys/module/oom settings on the host system.
Warning: Disabling OOM killer (--oom-kill-disable=true) can lead to system instability if the container consumes all available memory.

Was this article helpful?