หลักปฏิบัติที่ดีด้านความปลอดภัยและ DevOps

Docker Security

คู่มือการป้องกันความปลอดภัยใน 2025-2026 สำหรับ นักพัฒนาและ DevOps

คู่มือ Docker Security Hardening ครบวงจร: 10 หลักปฏิบัติที่ดี ป้องกัน Container จากการโจมตีด้วยหลัก CIS Benchmark และ Docker Black Hat 2025

Docker
ความปลอดภัย
CIS

โครงสร้าง Docker Security Lifecycle

Dev Development Build Dockerfile Multi-stage Scan Trivy / Snyk Deploy CI/CD Pipeline Security Gates Runtime Monitoring CIS Checks Security Alerts Root Non-Root 🔒 Locked 🛡️ CIS 🧪 Scan

บทนำ

Docker ได้กลายมาเป็นเทคโนโลยีมาตรฐานในการ deploy แอปพลิเคชันสมัยใหม่ แต่ความปลอดภัยของ Container ก็เป็นสิ่งที่ต้องให้ความสำคัญไม่แพ้กัน โดยเฉพาะหลังจากเหตุการณ์ Docker Black Hat 2025 ที่พบช่องโหว่จำนวนมาก ที่เกิดจากการ configuration ที่ไม่ปลอดภัย

76%
ของ DevOps teams ที่ใช้ Docker มีการบันทึก CVEs ใน images
85%
ของ misconfigurations เกิดจากการใช้ root user ใน container
92%
ของ organizations ใช้ CIS Docker Benchmark สำหรับ security audit

สิ่งที่ต้องเตรียม

Docker Engine

ติดตั้ง Docker Engine เวอร์ชันล่าสุด (24.x หรือสูงกว่า)

Linux/macOS/Windows

ระบบที่รองรับ Docker และ Docker Compose

พื้นฐาน Docker

เข้าใจคำสั่ง Docker พื้นฐาน เช่น docker run, docker build, docker-compose

Security Tools

ติดตั้ง Trivy หรือ Snyk สำหรับ scanning images

หลักการพื้นฐาน Docker Security

1. Principle of Least Privilege

Container ควรทำงานด้วย user ที่มีสิทธิ์น้อยที่สุดเท่าที่จะทำได้ หลีกเลี่ยงการใช้ root user ซึ่งเป็นหนึ่งในสาเหตุหลักของ security incidents

2. Defense in Depth

ใช้หลายชั้นของการป้องกัน: secure image, runtime security, network segmentation, และ monitoring ไม่พึ่งพาเพียงวิธีเดียว

3. Immutable Infrastructure

สร้าง container ใหม่แทนการแก้ไข container ที่มีอยู่ หากมีปัญหาให้ทำลาย container และสร้างใหม่ทั้งหมด

4. ความปลอดภัยอัตโนมัติ

ผสาน security scanning เข้ากับ CI/CD pipeline เพื่อค้นหา vulnerabilities ก่อน deployment

10 หลักปฏิบัติด้านความปลอดภัย Docker

01 ใช้ Base Image ที่เล็กและน่าเชื่อถือ

Base image คือพื้นฐานของ container ดังนั้นควรเลือก image ที่มีขนาดเล็ก และมีการอัปเดต security patches อย่างสม่ำเสมอ

ดี (Recommended)

bash
FROM alpine:3.18
WORKDIR /app
COPY --from=builder /app/app .

ไม่ดี (Not Recommended)

bash
FROM ubuntu:latest
RUN apt-get update && apt-get install -y \
    package1 package2 package3
ตัวอย่าง Base Images ที่ปลอดภัย:
  • Alpine Linux - Size ~5MB, security-focused
  • Distroless - No shell, minimal attack surface
  • DebianSlim - Only essential packages
  • CIS Docker Benchmarks - Compliant images

02 ใช้ Multi-Stage Dockerfile

Multi-stage build ช่วยลดขนาด image และตัด build tools ออกไป ทำให้ container มีขนาดเล็กและปลอดภัยกว่า

Dockerfile
FROM golang:1.20 as builder
WORKDIR /app
COPY . .
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o main .

FROM alpine:3.18
RUN apk --no-cache add ca-certificates
WORKDIR /root/
COPY --from=builder /app/main .
CMD ["./main"]

ขั้นตอนที่ 1 ใช้ for build (golang), ขั้นตอนที่ 2 สำหรับ runtime (alpine)

03 ใช้ Non-Root User

การรัน container ด้วย root user เป็นหนึ่งใน security risks ที่ใหญ่ที่สุด ควรสร้าง user ที่ไม่มีสิทธิ์ root และรันด้วย user นั้น

Dockerfile
FROM node:18-alpine

# Create non-root user
RUN addgroup -g 1001 -S nodejs
RUN adduser -S nodejs -u 1001 -G nodejs

# Change ownership
WORKDIR /app
COPY --chown=nodejs:nodejs package*.json ./
COPY --chown=nodejs:nodejs . .

USER nodejs

CMD ["node", "app.js"]
bash
# ตรวจสอบว่า container รันด้วย root หรือไม่
docker exec [container-id] whoami

# ตรวจสอบ UID
docker exec [container-id] id

04 Scan Images หา Vulnerabilities

ติดตั้ง Trivy หรือ Snyk เพื่อ scan images หา vulnerabilities ก่อน deploy ลง production

ติดตั้ง Trivy

bash
# Linux (Ubuntu/Debian)
wget -qO - https://aquasecurity.github.io/trivy-repo/deb/public.key | gpg --dearmor -o /usr/share/keyrings/trivy.gpg
echo "deb [signed-by=/usr/share/keyrings/trivy.gpg] https://aquasecurity.github.io/trivy-repo/deb any-version main" | tee -a /etc/apt/sources.list.d/trivy.list
apt update && apt install trivy

# Windows (Homebrew)
brew install aquasecurity/trivy/trivy

ใช้งาน Trivy

bash
# Scan local image
trivy image node:18-alpine

# Scan dengan severity ที่สนใจ
trivy image --severity HIGH,CRITICAL node:18-alpine

# Export results to JSON
trivy image --format json --output results.json node:18-alpine
bash
[INFO] Node.js (javascript)
Total: 0 (CRITICAL:0, HIGH:0, MEDIUM:0, LOW:0, UNKNOWN:0)

05 ใช้ Docker Security Tools

Trivy (Aqua Security)

  • Open source, free
  • เร็ว, accuracy สูง
  • รองรับ several image formats
  • รวม CI/CD pipeline support

Snyk Container

  • Developer-first security
  • GitHub integration
  • แนะนำ fix recommendations
  • Free tier available

Docker Bench for Security

  • CIS Docker Benchmark compliance
  • ตรวจสอบ Docker daemon configuration
  • หลักปฏิบัติที่ดีสำหรับ Container
  • ฟรีและ open source

Aqua Security

  • Enterprise-grade security
  • Network policy enforcement
  • Runtime protection
  • รองรับ Kubernetes

06 securing Docker Daemon

Docker daemon คือ component ที่สำคัญที่สุดของ Docker การ securing daemon เป็นสิ่งจำเป็นสำหรับ security

TLS Authentication

ใช้ TLS for Docker daemon communication เพื่อป้องกัน man-in-the-middle attacks

Docker daemon.json
{
  "tls": true,
  "tlsverify": true,
  "tlscacert": "/etc/docker/ca.pem",
  "tlscert": "/etc/docker/server-cert.pem",
  "tlskey": "/etc/docker/server-key.pem",
  "hosts": ["tcp://0.0.0.0:2376"]
}

restrict network exposure

bash
# อย่า expose Docker daemon ให้ public
# เฉพาะ localhost เท่านั้น
dockerd --host=unix:///var/run/docker.sock

# หรือ restrict port
dockerd --host=tcp://127.0.0.1:2375
คำเตือน:

การ expose Docker daemon ให้ public (0.0.0.0) เป็น security risk ที่รุนแรง ผู้โจมตีสามารถเข้าถึง host ได้ผ่าน Docker API

07 Runtime Security

Security ที่ดีต้องครอบคลุมทั้ง image และ runtime ใช้ security flags สำหรับ docker run

bash
# Secure docker run command
docker run --read-only \
    --cap-drop=ALL \
    --cap-add=NET_BIND_SERVICE \
    --no-new-privileges \
    --security-opt apparmor=docker-default \
    --security-opt seccomp=unconfined \
    --rm \
    -p 8080:8080 \
    myapp:latest
--read-only

Mount container filesystem เป็น read-only ป้องกันการ modifi

--cap-drop=ALL

ลบ Linux capabilities ทั้งหมด เพิ่ม capability ที่จำเป็นเท่านั้น

--no-new-privileges

ป้องกันการได้รับสิทธิ์เพิ่มเติมผ่าน setuid/setgid

--rm

ลบ container หลังจาก exit ไม่มี container ค้างในระบบ

08 การจัดการค่าความลับ

อย่า hardcode secrets (API keys, passwords) ใน Dockerfile หรือ code ใช้ Docker Secrets หรือเครื่องมือภายนอก

Docker Secrets (Swarm)

bash
# สร้าง secret
echo "my-secret-password" | docker secret create db_password -

# ดู secrets
docker secret ls

# ใช้ใน compose file
# docker-compose.yml
docker-compose.yml
version: "3.8"
services:
  app:
    image: myapp:latest
    secrets:
      - db_password
    environment:
      - DB_PASSWORD_FILE=/run/secrets/db_password

secrets:
  db_password:
    external: true

เครื่องมือภายนอก (แนะนำสำหรับ DevOps)

HashiCorp Vault
บริการจัดการความลับในระดับองค์กร
AWS Secrets Manager
โซลูชันจาก AWS
Infisical
Secrets สำหรับ DevOps แบบ open-source

09 ความปลอดภัยเครือข่าย

ใช้ network isolation เพื่อจำกัดการสื่อสารระหว่าง containers และป้องกัน lateral movement

เครือข่าย Bridge แบบกำหนดเอง

bash
# สร้าง network
docker network create app-network

# สร้าง container พร้อม network
docker run --network app-network \
    --name db \
    -e POSTGRES_PASSWORD=secret \
    -d postgres:15

# สร้าง app container
docker run --network app-network \
    --name app \
    -d myapp:latest
docker-compose.yml
version: "3.8"
services:
  app:
    image: myapp:latest
    networks:
      - app-network
  db:
    image: postgres:15
    networks:
      - app-network
    environment:
      - POSTGRES_PASSWORD_FILE=/run/secrets/db_password

networks:
  app-network:
    driver: bridge
Network Security Tips:
  • ใช้ isolated network สำหรับแต่ละ application
  • ไม่ expose ports ที่ไม่จำเป็น
  • ใช้ internal network สำหรับ database
  • Enable network policies (Kubernetes)

10 Update และ Patch อย่างสม่ำเสมอ

Security เป็น process ที่ต้องทำสม่ำเสมอ ไม่ใช่ just one-time task

bash
# อัปเดต Docker daemon
apt update && apt upgrade docker-ce

# ตรวจสอบ vulnerabilities ของ images
trivy image --severity HIGH,CRITICAL myapp:latest

# ลบ unused images
docker image prune -a --filter "until=72h"

CI/CD Security Pipeline Example

GitHub Actions
name: Security Scan
on: [push]
jobs:
  security-scan:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - name: Build image
        run: docker build -t myapp:latest .
      - name: Run Trivy scan
        run: |
          trivy image --severity HIGH,CRITICAL \
            --exit-code 1 \
            --no-progress myapp:latest
        continue-on-error: false

Case Study: From Leaky Container to Fort Knox

สร้างตัวอย่างการ hardening container แบบ step by step

ต้นฉบับ (Before Hardening)

FROM ubuntu:latest
RUN apt-get update && apt-get install -y curl wget

COPY . /app
WORKDIR /app

EXPOSE 8080
CMD ["./app"]
ubuntu:latest
root user
no scan
exposed ports

หลัง Hardening (After)

FROM golang:1.20-alpine AS builder
WORKDIR /app
COPY go.mod go.sum ./
RUN go mod download
COPY . .
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o main .

FROM alpine:3.18
RUN apk --no-cache add ca-certificates
RUN addgroup -g 1001 -S appgroup && adduser -S appuser -u 1001 -G appgroup

WORKDIR /app
COPY --from=builder /app/main .
USER appuser

EXPOSE 8080
CMD ["./main"]
alpine:3.18
non-root user
multi-stage
minimal deps

Security Improvements:

  • Size: 700MB → 25MB (ลด 96%)
  • CVEs: 42 → 0
  • User: root → appuser (UID 1001)
  • Layers: 15 → 7

CIS Docker Benchmark Checklist

CIS Docker Benchmark เป็น standard ที่ยอมรับกันทั่วโลกสำหรับ Docker security ใช้ตรวจสอบ configuration และ security posture

bash
# ติดตั้ง Docker Bench for Security
wget https://raw.githubusercontent.com/docker/docker-bench-security/main/docker-bench-security.sh
chmod +x docker-bench-security.sh

# Run scan
./docker-bench-security.sh

สำคัญ 10 ข้อที่ต้องเช็ค:

1.5 Ensure a logging driver is configured Critical
2.1 Docker Socket is not shared Critical
5.1 Ensure SELinux security options are set Important
5.2 Ensure Linux Kernel Capabilities are restricted Important
5.3 Ensure privileged containers are not used Important
5.4 Ensure sensitive host system directories are not mounted Important
5.5 Ensure sshd is not run inside containers Important
5.6 Ensure privileged ports are not mapped Important
5.7 Ensure size of container is tracked Important
5.8 Ensure additional process are not installed Important

Troubleshooting Common Security Issues

1. Container รันด้วย root

แก้ไข: ใช้ USER directive ใน Dockerfile

# Dockerfile
RUN addgroup -g 1001 appgroup
RUN adduser -S appuser -u 1001 -G appgroup
USER appuser

2. ภาพ size ใหญ่เกินไป

แก้ไข: ใช้ multi-stage build และ base image ที่เล็ก

# Multi-stage build
FROM builder-image AS builder
# ขั้นตอนการ build

FROM alpine:3.18
COPY --from=builder /app/app .
# รันไทม์ image ที่เล็กที่สุด

3. พบ vulnerabilities ใน image

แก้ไข: อัปเดต base image และ rebuild

# อัปเดต TO
FROM node:20-alpine3.18

# หรือ rebuild with patched version
docker build --no-cache -t myapp:latest .

4. Docker daemon security

แก้ไข: ใช้ TLS และจำกัด network access

# daemon.json
{
  "tls": true,
  "tlsverify": true,
  "hosts": [\"unix:///var/run/docker.sock\"]
}

สรุป & ขั้นตอนถัดไป

สรุปหลักการรักษาความปลอดภัย

  • 1 ใช้ Minimal Base Images (Alpine, Distroless)
  • 2 Multi-Stage Dockerfile
  • 3 Non-Root User ทุก container
  • 4 Scan vulnerabilities ด้วย Trivy/Snyk
  • 5 Drop Linux capabilities ทั้งหมด
  • 6 ใช้ Docker Secrets หรือ external tools
  • 7 แยกเครือข่ายตามความต้องการ
  • 8 Update และ patch อย่างสม่ำเสมอ
  • 9 Protect Docker daemon ด้วย TLS
  • 10 ใช้ CIS Docker Benchmark ให้ถูกต้อง

ขั้นตอนถัดไป

    1

    เริ่มต้นอย่างง่าย

    เริ่มจาก non-root user และ scan images

    2

    บูรณาการกับ CI/CD

    ผสาน security scanning เข้ากับ pipeline

    3

    ตรวจสอบสภาพแวดล้อม

    ใช้ Docker Bench for Security audit

    4

    ตรวจสอบใน Runtime

    ใช้ tools เช่น Falco, Sysdig