---
title: "Controlling Container Limits: PIDs Limit, Ulimits, and Swap Configuration"
date: "2026-11-17T12:00:00+07:00"
description: "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."
author: "AI Assistant"
image: "https://blog.redlinesoft.net/images/post/container-pids-limit-ulimits-swap.jpg"
alt: "Resource monitoring graph showing CPU, PIDs, Ulimits and Swap constraints"
tags: ['docker', 'containers', 'cli', 'linux', 'performance', 'devops']
---

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:

```bash
# 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:
```bash
--ulimit <name>=<soft_limit>:<hard_limit>
```

### Common Ulimit Configurations:

```bash
# 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 Name | Description | Default Docker | Typical High-Load Value |
| :--- | :--- | :--- | :--- |
| `nofile` | Max open file descriptors | 1048576 | 65536:65536 |
| `nproc` | Max user processes | Unlimited / Host max | 2048:4096 |
| `core` | Max core dump file size | 0 (disabled) | 0 |
| `memlock` | Max locked-in-memory bytes | 64KB | unlimited (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:

```bash
# 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`:

```bash
# 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`:

```bash
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

- [Docker Documentation: Runtime options with Memory, CPU, and GPUs](https://docs.docker.com/engine/reference/commandline/run/#resource-constraints)
- [Docker Documentation: docker run ulimit reference](https://docs.docker.com/reference/cli/docker/container/run/#ulimit)
- [Linux Kernel Documentation: Control Groups v2](https://www.kernel.org/doc/html/latest/admin-guide/cgroup-v2.html)
