1. บทนำ
Docker Bake เป็นเครื่องมือ orchestration สำหรับจัดการ Docker builds ที่ถูกปล่อยเวอร์ชัน GA (General Availability) ในเดือนกุมภาพันธ์ 2025 โดย Docker Team
ต่างจากการใช้ docker build แบบปกติ Docker Bake ช่วยให้เราจัดการ builds ได้จำนวนมากพร้อมกันด้วยไฟล์ configuration (HCL หรือ YAML) ซึ่งเหมาะกับ:
- Multi-target builds: Build หลาย images พร้อมกันในคำสั่งเดียว
- Multi-platform: Build สำหรับ various architectures (x86, ARM, etc.)
- Parallel builds: Build หลาย targets พร้อมกันเพื่อความเร็ว
- CI/CD integration: ใช้งานง่ายกับ GitHub Actions, GitLab CI
Docker Bake vs Docker BuildKit
Docker BuildKit เป็น backend engine ส่วน Docker Bake เป็น front-end orchestration tool ที่ใช้ BuildKit ในการ build ทำให้ได้ benefits ทั้งสองอย่าง เช่น build caching, parallel builds, และ multi-stage builds
2. สิ่งที่ต้องเตรียม
Hardware Requirements
- CPU: 2 cores minimum (4+ สำหรับ multi-platform)
- RAM: 4GB minimum (8GB แนะนำ)
- Disk: 10GB free space
- Network: สัญญาณอินเทอร์เน็ตที่เสถียร
Software Requirements
- Docker: v24.0+ (กับ Docker Bake baked-in)
- Git: สำหรับ version control
- Text Editor: VS Code, Sublime, etc.
- Registry: Docker Hub หรือ private registry
ความรู้พื้นฐานที่ควรรู้
- Docker basics (docker build, docker run)
- Dockerfile syntax และ multi-stage builds
- CI/CD concepts (GitHub Actions basics)
- ความเข้าใจพื้นฐานของ terminal/command line
3. การติดตั้ง Docker Bake
คำอธิบาย:
Docker Bake ได้รับการรวมเข้าไปใน Docker CLI ตั้งแต่เวอร์ชัน 24.0 แล้ว ดังนั้นคุณไม่ต้องติดตั้งแยกต่างหาก
ทางเลือก: เลือกวิธีใดวิธีหนึ่งที่เหมาะกับคุณ ไม่จำเป็นต้องทำทุกวิธี
Docker (Recommended)
โดย Docker ได้รวม Docker Bake เข้าไปใน CLI ตั้งแต่เวอร์ชัน 24.0 (released Feb 2025)
# Check Docker version (ต้องเป็น 24.0+)
$ docker --version
# Check docker bake command
$ docker bake --help
# Build all targets
$ docker bake
# Build specific target
$ docker bake target-name
# Build พร้อม push
$ docker bake --push
# List available targets
$ docker bake --print
Standalone Binary (For older Docker)
สำหรับ Docker เวอร์ชันเก่า (ก่อน 24.0) ที่ยังไม่มี bake command:
# Download Docker Bake standalone
$ wget https://github.com/docker/buildx/releases/download/v0.13.0/bake-v0.13.0-linux-amd64
# Make executable
$ chmod +x bake-v0.13.0-linux-amd64
# Move to PATH
$ sudo mv bake-v0.13.0-linux-amd64 /usr/local/bin/docker-bake
# Test installation
$ docker-bake --version
4. โครงสร้างไฟล์ docker-bake.hcl
Docker Bake รองรับทั้ง HCL (HashiCorp Configuration Language) และ YAML หรือ JSON configs โดยทั่วไปจะใช้ไฟล์ชื่อ docker-bake.hcl หรือ docker-bake.json:
โครงสร้าง Cơ bản
variable "APP_NAME" {
default = "myapp"
}
variable "APP_VERSION" {
default = "1.0.0"
}
variable "REGISTRY" {
default = "myregistry.com"
}
# Base target ที่ shared configuration
target "base" {
dockerfile = "./dockerfiles/Dockerfile.base"
tags = ["${REGISTRY}/${APP_NAME}:base-${APP_VERSION}"]
args = {
APP_VERSION = APP_VERSION
}
}
# Production target ( inherit from base )
target "production" {
inherits = ["base"]
dockerfile = "./dockerfiles/Dockerfile.prod"
tags = [
"${REGISTRY}/${APP_NAME}:prod-${APP_VERSION}",
"${REGISTRY}/${APP_NAME}:latest"
]
args = {
NODE_ENV = "production"
}
platforms = ["linux/amd64", "linux/arm64"]
}
# Development target
target "development" {
dockerfile = "./dockerfiles/Dockerfile.dev"
tags = ["${APP_NAME}:dev"]
target = "development"
contexts = {
src = "."
data = "./data"
}
output = ["type=cacheonly"]
}
# Multi-platform build
target "multi" {
dockerfile = "./dockerfiles/Dockerfile"
tags = ["${REGISTRY}/${APP_NAME}:multi"]
platforms = [
"linux/amd64",
"linux/arm64",
"linux/arm/v7"
]
}
# Push to registry
target "push" {
inherits = ["production"]
output = ["type=registry"]
}
HCL vs YAML
HCL (Default)
- Readable และเขียนง่าย
- รองรับ variables และ functions
- ใช้ใน Docker Official
YAML
- Familiar to DevOps engineers
- Better for CI/CD integration
- ใช้งานง่ายกับ existing tools
5. Multi-Target Builds
Multi-Target Builds เป็นหนึ่งในฟีเจอร์หลักของ Docker Bake ที่ช่วยให้เรา build หลาย targets พร้อมกันในคำสั่งเดียว ลดเวลาและ complexity ในการจัดการ Docker builds:
Multi-Target Flow Diagram
# ตัวอย่าง: Build หลาย targets พร้อมกัน
# Application target
target "app" {
dockerfile = "./Dockerfile"
tags = ["myregistry/myapp:latest"]
output = ["type=registry"]
}
# Test target ( reuse base image from app )
target "test" {
inherits = ["app"]
dockerfile = "./Dockerfile.test"
tags = ["myregistry/myapp:test"]
args = {
TEST_MODE = "true"
}
}
# Development target ( interactive, with dev tools )
target "dev" {
dockerfile = "./Dockerfile.dev"
tags = ["myregistry/myapp:dev"]
target = "development"
output = ["type=cacheonly"]
attributes = ["interactive=true"]
}
# Build all targets
$ docker bake
# Build specific targets
$ docker bake app test
# Build targets พร้อม parallel execution
$ docker bake --parallel app test dev
Parallel Builds Performance
Docker Bake สามารถ build หลาย targets พร้อมกันโดยอัตโนมัติ โดยใช้ parallel execution ทำให้ลดเวลา build ได้อย่างมีนัยสำคัญ:
| Scenario | Sequential Build | Parallel (Docker Bake) |
|---|---|---|
| 3 targets, 2min each | ~6 minutes | ~2 minutes |
| 5 targets, 3min each | ~15 minutes | ~5 minutes |
| 10 targets, varies | ~30+ minutes | ~10-15 minutes |
6. Multi-Platform Builds
Docker Bake สามารถ build สำหรับ various platforms ได้ในเวลานับวินาที ด้วยการใช้ BuildKit's built-in support for multi-platform builds:
Architecture Overview
target "multi-platform" {
dockerfile = "./Dockerfile"
tags = [
"myregistry/myapp:latest",
"myregistry/myapp:v1.0.0"
]
# Support multiple platforms
platforms = [
"linux/amd64",
"linux/arm64",
"linux/arm/v7",
"linux/s390x",
"linux/ppc64le"
]
# Enable build cache
cache-from = ["type=gha"]
cache-to = ["type=gha,mode=max"]
# Push to registry
output = ["type=registry"]
}
รองรับ Multi-Platform หลัก
7. GitHub Actions Integration
Docker Bake ทำงานร่วมกับ GitHub Actions ได้อย่างสมบูรณ์แบบสำหรับ CI/CD pipeline โดยคุณสามารถกำหนด targets ในไฟล์ docker-bake.hcl แล้วเรียกใช้ผ่าน workflow:
CI/CD Flow
name: CI/CD Pipeline
on:
push:
branches: [ main, develop ]
pull_request:
branches: [ main ]
env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}
jobs:
build-and-push:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Login to Container Registry
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Build and push Docker images
uses: docker/bake-action@v4
with:
targets: production multi
push: true
files: |
docker-bake.hcl
docker-bake-dev.hcl
- name: Run tests
run: docker run myapp:test npm test
- name: Deployment
if: github.ref == 'refs/heads/main'
run: |
echo "Deploying to production..."
docker-bake.hcl สำหรับ GitHub Actions
# github-bake.hcl - สำหรับ GitHub Actions
# Default target
target "default" {
dockerfile = "./Dockerfile"
tags = ["ghcr.io/${{ github.repository }}:latest"]
}
# Production target
target "production" {
dockerfile = "./Dockerfile"
tags = [
"ghcr.io/${{ github.repository }}:prod-${{ github.sha }}",
"ghcr.io/${{ github.repository }}:prod-latest"
]
platforms = ["linux/amd64", "linux/arm64"]
output = ["type=registry"]
cache-from = ["type=gha"]
cache-to = ["type=gha,mode=max"]
}
# Multi-platform target
target "multi" {
inherits = ["production"]
tags = ["ghcr.io/${{ github.repository }}:multi-${{ github.sha }}"]
platforms = ["linux/amd64", "linux/arm64", "linux/arm/v7"]
}
# Test target
target "test" {
inherit = ["default"]
dockerfile = "./Dockerfile.test"
tags = ["ghcr.io/${{ github.repository }}:test-${{ github.sha }}"]
output = ["type=cacheonly"]
}
8. Best Practices
ข้อควรระวังและแนวทางปฏิบัติที่ดี
ที่ควรทำ ✓
- ใช้ variables ในการจัดการ configuration
- สร้าง base target เพื่อ shared config
- ใช้ inherits เพื่อ reduce duplication
- ตั้งค่า cache-from/cache-to สำหรับ build speed
- ใช้ --parallel flag สำหรับ multiple targets
- แยก config ตาม environment (dev, staging, prod)
- ใช้ git tags สำหรับ image versioning
ที่ควรหลีกเลี่ยง ✗
- หลีกเลี่ยง hard-coded values ใน config
- ไม่ควร build ทุก targets ทุกครั้ง
- หลีกเลี่ยงการ build หลาย platforms พร้อมกันมากเกินไป
- ไม่ควรใช้ cache-only output ใน CI/CD
- หลีกเลี่ยงการเปลี่ยน target บ่อยๆ ระหว่าง builds
- ไม่ควรเก็บ credentials ใน config file
- หลีกเลี่ยงการ build ใหญ่ๆ โดยไม่มี testing
Performance Optimization Tips
1. Docker Build Caching
ใช้ build cache เพื่อลด build time โดยเฉพาะใน CI/CD:
cache-from = ["type=gha"]และ
cache-to = ["type=gha,mode=max"]
2. Build Parallelization
Docker Bake ช่วย build หลาย targets พร้อมกัน แต่ไม่ควรส่งมากเกินไปใน same time:
docker bake --parallel --max-jobs=4
3. Multi-stage Dockerfile
ใช้ multi-stage builds เพื่อลด image size และ build time ของ each target
9. สรุป
Docker Bake เป็นเครื่องมือที่ทรงพลังสำหรับจัดการ Docker builds แบบ modern architecture โดยเฉพาะใน CI/CD pipelines
Key Takeaways
- Docker Bake GA ใน Feb 2025 และรวมอยู่ใน Docker CLI 24.0+
- รองรับ HCL และ YAML configs สำหรับ flexibility
- Multi-target builds พร้อมการ reuse configurations
- Multi-platform builds เช่น linux/amd64, linux/arm64
- ทำงานร่วมกับ GitHub Actions ได้อย่างสมบูรณ์
- Built-in caching และ build optimization
ด้วยความสามารถในการ orchestrate multiple builds, support multi-platform deployments และ integration กับ modern CI/CD tools Docker Bake จึงเป็นเครื่องมือที่ควรเรียนรู้และใช้งานสำหรับ DevOps engineers และ cloud native developers