Infrastructure, Network and Docker

Secure code on an insecure server is still insecure.

Tools

ToolUse
trivyScans Docker images, IaC and repos for CVEs and misconfigurations
hadolintDockerfile linter
checkov / tfsecTerraform / IaC security scanning
Cloudflare / AWS WAFWeb Application Firewall + DDoS protection
Nginx / API gateway (Kong, AWS API Gateway)TLS, rate limits, auth at the edge

Network

  • The DB and Redis sit in private subnets, never public (0.0.0.0/0 only on 443 at the load balancer)
  • Security groups: the app accepts traffic only from the LB; the DB accepts only from the app
  • Admin, SSH and dashboards sit behind a VPN / bastion / SSO, or use AWS SSM instead of SSH
  • Zero trust: services authenticate each other (mTLS, service mesh)
  • Restrict egress where possible (helps against SSRF and data exfiltration)

Least privilege

-- App DB user: no DROP, no superuser
CREATE USER app WITH PASSWORD '...';
GRANT SELECT, INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA public TO app;
  • Cloud IAM roles per service, scoped to specific resources (no "Resource": "*")
  • No root/admin keys used by apps

✅ Secure Dockerfile

FROM node:22-alpine AS build
WORKDIR /app
COPY package*.json ./
RUN npm ci --omit=dev --ignore-scripts
COPY . .

FROM gcr.io/distroless/nodejs22-debian12   # minimal: no shell, no package manager
WORKDIR /app
COPY --from=build /app .
USER nonroot                                # never run as root
ENV NODE_ENV=production
CMD ["server.js"]

.dockerignore:

.env
.git
node_modules
*.pem
trivy image myapp:latest
docker run --read-only --cap-drop=ALL --security-opt=no-new-privileges myapp

Kubernetes basics

runAsNonRoot: true, readOnlyRootFilesystem: true, allowPrivilegeEscalation: false, NetworkPolicies, Secrets from an external secret manager, resource limits.

Backups and recovery

  • Automated, encrypted backups in a separate account/region
  • Test restores regularly; a backup you can't restore is not a backup
  • Keep an immutable copy (protects against ransomware)

Checklist

  • Only the LB is public; DB/cache are private
  • TLS 1.2+ with auto-renewing certificates (Let's Encrypt / ACM)
  • Containers run as non-root, from minimal images, scanned in CI
  • IAM least privilege; MFA on cloud accounts
  • OS and runtime patched
  • Backups encrypted and restore-tested
  • An incident response plan written down