Skip to content
Blog

Controlling Container Limits: PIDs Limit, Ulimits, and Swap Configuration

A complete reference to restricting container kernel resources: preventing fork bombs with --pids-limit, tuning file descriptors and processes with --ulimit, and managing --memory-swap.

Published on • November 17, 2026

AI Assistant

Unbounded containers pose significant stability risks to multi-tenant hosts. A buggy microservice or malicious payload can consume all available process threads (fork bomb), exhaust host file handles (nofile), or cause disk thrashing due to excessive virtual memory swapping.

Linux Control Groups (cgroups) and kernel POSIX resource limits (ulimits) allow Docker operators to place strict upper bounds on container execution limits.


1. Preventing Fork Bombs with --pids-limit

A fork bomb occurs when a malicious or infinite loop process repeatedly spawns sub-processes until host process ID (PID) table allocations are exhausted, crashing the host OS.

Docker provides the --pids-limit flag to restrict the total number of active process IDs inside a container’s cgroup:

# Limit the container to a maximum of 100 concurrent processes
docker run -d --name secure-service \
  --pids-limit 100 \
  my-app:v1

If processes inside the container attempt to execute fork() beyond 100 processes, the kernel denies the call with Resource temporarily unavailable.

Recommended Baseline: Web services typically require 50–200 PIDs. Set --pids-limit 500 for general workloads to prevent host-wide thread starvation.


2. Tuning POSIX Limits with --ulimit

Linux applications (such as high-concurrency databases, Redis, or NGINX) often require custom system limits for file descriptors (nofile) or maximum user processes (nproc).

Docker allows setting kernel ulimits per container using --ulimit:

Syntax:

--ulimit <name>=<soft_limit>:<hard_limit>

Common Ulimit Configurations:

# Increase open file descriptor limits (nofile) for database workloads
docker run -d --name redis-cache \
  --ulimit nofile=65536:65536 \
  redis:alpine

# Restrict maximum process creations per user (nproc)
docker run -d --name worker-app \
  --ulimit nproc=1024:2048 \
  worker-image:latest

Table of Frequent Ulimits:

Ulimit NameDescriptionDefault DockerTypical High-Load Value
nofileMax open file descriptors104857665536:65536
nprocMax user processesUnlimited / Host max2048:4096
coreMax core dump file size0 (disabled)0
memlockMax locked-in-memory bytes64KBunlimited (e.g. for Elasticsearch)

3. Swap Memory Management with --memory-swap

By default, Docker allows containers to use swap space up to double their RAM allocation if host swap space is available. Excessive swap usage slows application response times down to disk I/O speeds.

Controlling Memory and Swap:

# Allocate 512MB RAM and 1GB total (512MB RAM + 512MB Swap)
docker run -d --name app \
  --memory 512m \
  --memory-swap 1g \
  my-app:latest

Disabling Swap Entirely:

To prevent a latency-sensitive application (such as Kubernetes pods or databases) from swapping to disk, set --memory-swap equal to --memory:

# Disables swap (RAM = 1GB, Swap = 0MB)
docker run -d --name no-swap-app \
  --memory 1g \
  --memory-swap 1g \
  my-app:latest

Rule: --memory-swap represents total memory + swap combined. Setting --memory-swap equal to --memory results in zero swap usage.


4. Host Kernel and Cgroups v2 Compatibility

Modern Linux distributions use cgroups v2, which unifies system resource accounting under /sys/fs/cgroup.

You can inspect the applied kernel limits of a running container using docker inspect:

docker inspect --format='PidsLimit: {{.HostConfig.PidsLimit}}, MemorySwap: {{.HostConfig.MemorySwap}}' secure-service

Ensure your host kernel supports memory swap accounting (cgroup_enable=memory swapaccount=1 boot parameters on older Linux kernel builds).


5. Summary and Best Practices

Takeaway: Protect multi-tenant host infrastructure against resource exhaustion by capping PID counts with --pids-limit, raising open file descriptor thresholds using --ulimit nofile=65536, and disabling performance-degrading swap with --memory-swap set equal to --memory.


Sources