Interview's MateDocker
Interview's Mate

Docker

Compose · Networking · Security · Ops

Visualizer

DSA Visualizer
All 32 lessons
Custom Playground
Try your own examples

Learning

System Design
31 architecture topics
SQL Learning
160 foundations → production topics
AI Engineering
160 LLM → backend topics
Data Science
160 Python → MLOps topics
Data Engineer
160 SQL → streaming topics
Docker
160 Docker learning topics
Kubernetes
160 K8s fundamentals → cloud
Java LearnSoon
Coming soon
Python Learn
Interview Q&A & internals
JavaScript Learn
Closures, async & DOM Q&A
Tech Learn
Search, practise & track Q&A
AI DSA Practice
Solve coding problems
Theme
Home
Docker topics
Lesson 118/ 160
06. Docker Security & Hardening

Protecting Against Fork Bombs & DoS (--pids-limit, --memory, --cpus)

Cap PIDs, memory, and CPUs so one container cannot DoS the host.

Intermediatepids-limit · memory3 quiz questions
Practice DevOps Q&A
In plain wordsDiagramTheoryStep by stepWhen to use itInterview cluesMistakes to avoidQuiz (3)Practice

In plain words

Apply bounded limits from measured needs and alert on pids, throttling and OOM before the only clue is host instability.

Think of it like…

These controls are circuit breakers: maximum occupants, load rating and electrical amperage stop one appliance from taking the whole building offline.

Where you’ve seen it

In practice, docker run --pids-limit=128 --memory=512m --memory-swap=512m --cpus=1 --name guarded api:2.1 is the concrete move for this job: cap PIDs, memory, and CPUs so one container cannot DoS the host. The release is not accepted until run a bounded fork/thread and allocation test, inspect pids.current, memory.events and cpu.stat, then confirm app load stays healthy confirms the result.

Words you’ll hear

pids-limit
The implementation control in docker run --pids-limit=128 --memory=512m --memory-swap=512m --cpus=1 --name guarded api:2.1 for cap PIDs, memory, and CPUs so one container cannot DoS the host.
memory
The result to observe with run a bounded fork/thread and allocation test, inspect pids.current, memory.events and cpu.stat, then confirm app load stays healthy, rather than infer from a successful command alone.
cpus
The production boundary exposed when setting --memory to 512m while the JVM reserves multi-gigabyte heap and native memory, or testing only CPU and ignoring logging process I/O.

Visual diagram

Loading interactive diagram

Theory & deep dive

pids-limit control path

The concrete mechanism is docker run --pids-limit=128 --memory=512m --memory-swap=512m --cpus=1 --name guarded api:2.1. The observable result is run a bounded fork/thread and allocation test, inspect pids.current, memory.events and cpu.stat, then confirm app load stays healthy; if those disagree, inspect the runtime evidence before changing unrelated settings.

Cap PIDs, memory, and CPUs so one container cannot DoS the host. Keep pids-limit, memory and cpus in the same deployment record so the intent and the observed state cannot drift apart.

Concrete pids-limit setup

docker run --pids-limit=128 --memory=512m --memory-swap=512m --cpus=1 --name guarded api:2.1

Key takeaway: Prove memory with run a bounded fork/thread and allocation test, inspect pids.current, memory.events and cpu.stat, then confirm app load stays healthy, not with command success alone.

cpus failure boundary

A pids cap can break thread-heavy JVM/Go workloads, while memory limits lead to OOM kill rather than a graceful app response. CPU shares differ from hard quota; unlimited Docker settings allow a noisy container to create system-wide denial of service. The narrow failure to design for is setting --memory to 512m while the JVM reserves multi-gigabyte heap and native memory, or testing only CPU and ignoring logging process I/O.

Accept this compromise explicitly: Strict resource caps protect the host; small buffers trigger latency and OOM, so load testing and headroom are part of the configuration. The controlled check-run a bounded fork/thread and allocation test, inspect pids.current, memory.events and cpu.stat, then confirm app load stays healthy-also identifies the point where a different design is safer.

Deliberate failure test

Exercise this failure safely: setting --memory to 512m while the JVM reserves multi-gigabyte heap and native memory, or testing only CPU and ignoring logging process I/O.

Key takeaway: Strict resource caps protect the host; small buffers trigger latency and OOM, so load testing and headroom are part of the configuration.

How it works, step by step

Step 1 of 4
Design

1. State the boundary

Write down why the workload needs this: A pids cap can break thread-heavy JVM/Go workloads, while memory limits lead to OOM kill rather than a graceful app response. CPU shares differ from hard quota; unlimited Docker settings allow a noisy container to create system-wide denial of service.

Apply bounded limits from measured needs and alert on pids, throttling and OOM before the only clue is host instability.
Key numbers at this step
Intent
pids-limit

When to use it & trade-offs

What it is

--pids-limit caps process/task creation, --memory bounds the cgroup memory ceiling, and --cpus applies CPU quota. Together they prevent a fork bomb or memory leak in one container from exhausting host resources, provided the host also reserves capacity for Docker and logging.

Why it matters: A pids cap can break thread-heavy JVM/Go workloads, while memory limits lead to OOM kill rather than a graceful app response. CPU shares differ from hard quota; unlimited Docker settings allow a noisy container to create system-wide denial of service.

When to reach for it

  • The fit is strongest for this job: cap PIDs, memory, and CPUs so one container cannot DoS the host. Pair it with run a bounded fork/thread and allocation test, inspect pids.current, memory.events and cpu.stat, then confirm app load stays healthy in the runbook.
  • Production pids-limit changes should make memory observable through docker run --pids-limit=128 --memory=512m --memory-swap=512m --cpus=1 --name guarded api:2.1, rather than trusting the runtime default.
  • Keep a rollback example for the case where setting --memory to 512m while the JVM reserves multi-gigabyte heap and native memory, or testing only CPU and ignoring logging process I/O.

When to avoid or be careful

  • Do not accept command success while the system can still fail by setting --memory to 512m while the JVM reserves multi-gigabyte heap and native memory, or testing only CPU and ignoring logging process I/O.
  • Avoid erasing cpus protection merely to dodge this cost: “Strict resource caps protect the host; small buffers trigger latency and OOM, so load testing and headroom are part of the configuration”.
  • Do not replace the evidence path-run a bounded fork/thread and allocation test, inspect pids.current, memory.events and cpu.stat, then confirm app load stays healthy-with a screenshot or a mutable tag.

docker run --pids-limit=128 --memory=512m --memory-swap=512m --cpus=1 --name guarded api:2.1: practical choices

Explicit pids-limit
Best for: Cap PIDs, memory, and CPUs so one container cannot DoS the host, with a reviewed runbook.

Strengths: --pids-limit caps process/task creation, --memory bounds the cgroup memory ceiling, and --cpus applies CPU quota. Together they prevent a fork bomb or memory leak in one container from exhausting host resources, provided the host also reserves capacity for Docker and logging.

Watch out: Strict resource caps protect the host; small buffers trigger latency and OOM, so load testing and headroom are part of the configuration.

Ambient default
Best for: Short-lived memory experiments with low blast radius.

Strengths: It can begin with docker run --pids-limit=128 --memory=512m --memory-swap=512m --cpus=1 --name guarded api:2.1 and fewer surrounding controls.

Watch out: It silently accepts setting --memory to 512m while the JVM reserves multi-gigabyte heap and native memory, or testing only CPU and ignoring logging process I/O, which makes the result hard to reproduce after promotion.

Managed cpus
Best for: Teams that centralize cpus policy across many workloads.

Strengths: A pids cap can break thread-heavy JVM/Go workloads, while memory limits lead to OOM kill rather than a graceful app response. CPU shares differ from hard quota; unlimited Docker settings allow a noisy container to create system-wide denial of service.

Watch out: The platform adds an ownership and debugging layer; retain run a bounded fork/thread and allocation test, inspect pids.current, memory.events and cpu.stat, then confirm app load stays healthy as the direct check.

Interview signals & numbers to know

When this topic comes up

  • docker run --pids-limit=128 --memory=512m --memory-swap=512m --cpus=1 --name guarded api:2.1
  • run a bounded fork/thread and allocation test, inspect pids.current, memory.events and cpu.stat, then confirm app load stays healthy
  • Failure signature: setting --memory to 512m while the JVM reserves multi-gigabyte heap and native memory, or testing only CPU and ignoring logging process I/O
  • Trade-off: Strict resource caps protect the host; small buffers trigger latency and OOM, so load testing and headroom are part of the configuration.

Benchmarks & rules of thumb

128 tasks -
max process/thread count in the example
512 MiB -
memory and swap ceiling in the example
1 CPU -
hard quota despite idle host capacity

Common mistakes & follow-ups

Common interview mistakes

  • Treating a successful docker run --pids-limit=128 --memory=512m --memory-swap=512m --cpus=1 --name guarded api:2.1 as proof and skipping run a bounded fork/thread and allocation test, inspect pids.current, memory.events and cpu.stat, then confirm app load stays healthy.
  • Setting --memory to 512m while the JVM reserves multi-gigabyte heap and native memory, or testing only CPU and ignoring logging process I/O
  • Optimizing pids-limit without accepting that strict resource caps protect the host; small buffers trigger latency and OOM, so load testing and headroom are part of the configuration.

Likely interviewer follow-ups

  • Why does docker run --pids-limit=128 --memory=512m --memory-swap=512m --cpus=1 --name guarded api:2.1 address pids-limit here rather than through application code?
  • How would you distinguish healthy memory from the failure “setting --memory to 512m while the JVM reserves multi-gigabyte heap and native memory, or testing only CPU and ignoring logging process I/O” using run a bounded fork/thread and allocation test, inspect pids.current, memory.events and cpu.stat, then confirm app load stays healthy?
  • When would this trade-off-“Strict resource caps protect the host; small buffers trigger latency and OOM, so load testing and headroom are part of the configuration”-justify a different cpus design?
Interview tip: Give one concrete operation-docker run --pids-limit=128 --memory=512m --memory-swap=512m --cpus=1 --name guarded api:2.1-and one proof-run a bounded fork/thread and allocation test, inspect pids.current, memory.events and cpu.stat, then confirm app load stays healthy. Then name the boundary: setting --memory to 512m while the JVM reserves multi-gigabyte heap and native memory, or testing only CPU and ignoring logging process I/O.

Knowledge check

Loading quiz

Put it into practice

Practice related interview Q&A

Apply Protecting Against Fork Bombs & DoS (--pids-limit, --memory, --cpus) with generative AI interview Q&A and hands-on practice.

Open DevOps Q&A →Tech Learn Q&A
Previous topic
Trusted Base Images & Docker Content Trust (DCT)
Next topic
CIS Docker Benchmark Compliance & Auditing (docker-bench-security)