Learn Docker Help

Docker Performance Optimization πŸš€

╔════════════════════════════════════════════════════╗ β•‘ DOCKER PERFORMANCE OPTIMIZATION β•‘ β•‘ β•‘ β•‘ CPU | Memory | I/O | Network | Build | Runtime β•‘ β•šβ•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•

Resource Management πŸ“Š

CPU Optimization

# Limitar CPU docker run --cpus=".5" nginx # Definir CPU shares docker run --cpu-shares=512 nginx # Pinning em CPUs especΓ­ficas docker run --cpuset-cpus="0,1" nginx

Memory Management

# Limitar memΓ³ria docker run --memory="512m" nginx # Configurar swap docker run --memory-swap="1g" nginx # Reservar memΓ³ria docker run --memory-reservation="256m" nginx

Storage Performance πŸ’Ύ

Storage Driver Selection

{ "storage-driver": "overlay2", "storage-opts": [ "overlay2.override_kernel_check=true" ] }

Volume Performance

# Volume local otimizado docker volume create --driver local \ --opt type=tmpfs \ --opt device=tmpfs \ --opt o=size=1g \ fast-volume # Monitorar I/O docker stats --format "table {{.Name}}\t{{.BlockIO}}"

Network Optimization 🌐

Network Mode Selection

# Host networking docker run --network host nginx # Performance tuning docker run \ --network-alias=fastnet \ --dns=8.8.8.8 \ nginx

Network Metrics

# Monitorar trΓ‘fego docker stats --format "table {{.Name}}\t{{.NetIO}}" # AnΓ‘lise detalhada docker run --rm \ --net container:target-container \ nicolaka/netshoot iftop

Build Performance ⚑

Multi-stage Builds

# Build stage FROM golang:1.19 AS builder WORKDIR /app COPY . . RUN CGO_ENABLED=0 go build -o app # Production stage FROM alpine:3.14 COPY --from=builder /app/app / CMD ["/app"]

Cache Optimization

# Usar buildkit export DOCKER_BUILDKIT=1 # Cache em registry docker buildx build \ --cache-from type=registry,ref=user/app:cache \ --cache-to type=registry,ref=user/app:cache \ .

Runtime Performance πŸ”„

Container Stats

# MΓ©tricas em tempo real docker stats --no-stream # Formato personalizado docker stats --format \ "table {{.Container}}\t{{.CPUPerc}}\t{{.MemUsage}}\t{{.NetIO}}"

Resource Quotas

version: '3.8' services: app: deploy: resources: limits: cpus: '0.50' memory: 512M reservations: cpus: '0.25' memory: 256M

Monitoring Tools πŸ“ˆ

Prometheus Integration

metrics: prometheus: enabled: true port: 9323

Grafana Dashboard

# Container metrics rate(container_cpu_usage_seconds_total{container_name!=""}[5m]) sum(container_memory_usage_bytes) by (container_name)

Performance Testing πŸ”

Load Testing

# Apache Benchmark ab -n 1000 -c 10 http://localhost:8080/ # Hey HTTP load generator hey -n 2000 -c 50 http://localhost:8080/

Profiling

# CPU profile docker run --cap-add=SYS_PTRACE \ --security-opt seccomp=unconfined \ app perf record -F 99 -p 1 # Memory profile docker run -p 6060:6060 app go tool pprof http://localhost:6060/debug/pprof/heap

Best Practices Checklist βœ…

Container Level

  1. [x] Resource limits configurados

  2. [x] Logging driver otimizado

  3. [x] Network mode apropriado

  4. [x] Volume mounts eficientes

  5. [x] Image size otimizado

Host Level

  1. [x] Storage driver adequado

  2. [x] Sistema de arquivos otimizado

  3. [x] Kernel parameters tuned

  4. [x] Network stack otimizado

  5. [x] Monitoramento ativo

Performance Troubleshooting Matrix 🎯

Sintoma

Métrica

Comando

Solução

CPU Alto

container_cpu_usage

docker stats

Ajustar limites

Memória Leak

container_memory_usage

docker stats

Heap analysis

I/O Lento

container_fs_io_current

iostat

Storage driver

Network Lag

container_network_transmit

iftop

Network mode

Advanced Tuning πŸ”§

Kernel Parameters

# Ajustar sysctls docker run --sysctl net.core.somaxconn=1024 nginx # Configurar ulimits docker run --ulimit nofile=65535:65535 nginx

Runtime Flags

# Otimizar garbage collector docker run -e "GOGC=100" app # Tuning JVM docker run -e "JAVA_OPTS=-XX:+UseG1GC" java-app

Waifu Performance Tips πŸ’«

Quick Reference πŸ“š

Common Commands

# CPU profile docker stats --format "table {{.Container}}\t{{.CPUPerc}}" # Memory analysis docker stats --format "table {{.Container}}\t{{.MemUsage}}" # I/O metrics docker stats --format "table {{.Container}}\t{{.BlockIO}}" # Network stats docker stats --format "table {{.Container}}\t{{.NetIO}}"

Performance Tools

  1. cAdvisor

  2. Prometheus

  3. Grafana

  4. netdata

  5. ctop

16 abril 2025