8. Docker CPU, Memory & Security

๐Ÿง  CPU, Memory & Security

๐Ÿ”น Why CPU & Memory Limits Matter

By default, Docker containers can use as much CPU and RAM as the host has.
That sounds flexible, but it’s dangerous.

Imagine one buggy app:

That’s why Docker gives resource control.

Two important terms:

๐Ÿ‘‰ Reservation = “at least this much”
๐Ÿ‘‰ Limit = “never more than this”


โš™๏ธ CPU & Memory Units (Must Know)

CPU in Docker

Example meaning:


CPU in Kubernetes (FYI)


Memory Units

๐Ÿ“Œ Golden rule:
Never allocate 100% of host resources.
Always keep 20–30% for OS + Docker daemon.


๐Ÿงช Practical Docker Resource Examples

Memory Reservation + Hard Limit

docker run -d --name web1 \
  --memory-reservation=256m \
  --memory=512m httpd
docker run -d --name web1 \
  --memory-reservation=256m \
  --memory=512m httpd

Meaning:


Simple CPU Limit

docker run -d --name web2 --cpus="0.5" httpd
docker run -d --name web2 --cpus="0.5" httpd

Meaning:


CPU Priority (Shares)

docker run -d --name web3 --cpu-shares=512 httpd
docker run -d --name web3 --cpu-shares=512 httpd

Meaning:

Good for:


Advanced CPU Control

docker run -d --cpu-period=100000 --cpu-quota=50000 httpd
docker run -d --cpu-period=100000 --cpu-quota=50000 httpd

Calculation:

Used when you need precise tuning.


Memory + Swap Control

docker run -d --memory=512m --memory-swap=512m httpd
docker run -d --memory=512m --memory-swap=512m httpd

Meaning:


๐Ÿ“Š Monitoring & Observability

Live Container Usage

docker stats
docker stats

Shows:


Inspect Limits

docker inspect web1
docker inspect web1

Look for:


Host-Level Tools

top
htop
free -m
top
htop
free -m

๐Ÿ‘‰ Always monitor both Docker + host.


๐Ÿงฎ Real Planning Example (Interview Favorite)

Host:

Reserve 30% for OS:

Run 4 containers:

Result:


๐Ÿ” Docker Security – Why It’s Critical

Even with limits, containers can still be dangerous if hacked.

A bad container can:

Security = layered protection, not one command.


๐Ÿ›ก๏ธ Docker Security Best Practices (With Meaning)

Use Minimal Base Images

Smaller image = fewer vulnerabilities.

FROM alpine:3.18
FROM alpine:3.18

Never Run as Root

RUN adduser -D appuser
USER appuser
RUN adduser -D appuser
USER appuser

If container is hacked → attacker gets limited user, not root.


Keep Images Updated

Old images = known CVEs.
Rebuild regularly:

docker pull nginx
docker build --no-cache .
docker pull nginx
docker build --no-cache .

Use Multi-Stage Builds

Builder has tools.
Final image has only the app.

Smaller + safer.


Drop Linux Capabilities

docker run --cap-drop=ALL nginx
docker run --cap-drop=ALL nginx

Removes unnecessary privileges.


Prevent Privilege Escalation

docker run --security-opt=no-new-privileges nginx
docker run --security-opt=no-new-privileges nginx

Even if exploited → cannot gain extra rights.


Resource Limits = DoS Protection

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

Stops runaway attacks.


Network Hygiene


Docker Secrets (Swarm)

Never store passwords in ENV.
Use secrets instead.


Image Scanning

Scan before deploy:


One Process Per Container

Avoid:


๐Ÿ”’ Secure Docker Run (Real Production Style)

docker run -d --name safe-app \
  --memory=512m --cpus="0.5" \
  --security-opt=no-new-privileges \
  --cap-drop=ALL \
  --read-only \
  --tmpfs /tmp:rw,size=64m \
  myuser/app:1.0
docker run -d --name safe-app \
  --memory=512m --cpus="0.5" \
  --security-opt=no-new-privileges \
  --cap-drop=ALL \
  --read-only \
  --tmpfs /tmp:rw,size=64m \
  myuser/app:1.0

What this gives you: