Git และ Docker เป็นคู่เทคโนโลยีที่สำคัญในการจัดการ Docker Projects ด้วย Version Control ที่มีประสิทธิภาพ การใช้งาน Git ร่วมกับ Docker ช่วยให้ทีมพัฒนาสามารถควบคุมเวอร์ชันของ Dockerfile, docker-compose.yml และ Application Code ได้อย่างเป็นระเบียบ ซึ่งจะช่วยให้การ Deploy และ Maintenance ของ Docker Containers เป็นไปได้อย่างราบรื่น บทความนี้จะช่วยให้คุณเข้าใจวิธีการใช้ Git ร่วมกับ Docker สำหรับจัดการ Project อย่างมี Discipline
ไฟล์สำคัญใน Docker Project ที่ต้อง Commit ลงใน Git
เมื่อทำงานกับ Docker Project จำเป็นต้องเก็บไฟล์ต่อไปนี้ใน Git Repository:
- Dockerfile – ไฟล์หลักที่กำหนดวิธีการสร้าง Docker Image
- docker-compose.yml – ไฟล์ที่กำหนด Multi-container Architecture
- .env.example – ตัวอย่างไฟล์ Environment Variables (ไม่มี sensitive data)
- Shell Scripts – Build และ Deploy Scripts สำหรับ Automation
- Application Code – Source code ของแอปพลิเคชันทั้งหมด
.gitignore สำหรับ Docker Projects
สิ่งสำคัญคือการกำหนด .gitignore อย่างถูกต้องเพื่อไม่ให้ Git ติดตามไฟล์ที่ไม่จำเป็น โดยเฉพาะไฟล์ที่อาจมี Sensitive Information:
# Docker related files
.docker/
docker-compose.override.yml
.env
.env.local
.env.*.local
# Build artifacts
build/
*.class
*.jar
*.pyc
__pycache__/
# Logs
logs/
*.log
npm-debug.log*
yarn-error.log*
# Dependencies
node_modules/
venv/
env/
.venv/
venv3/
# IDE and Editor
.vscode/
.idea/
*.swp
*.swo
*.sublime-*
.DS_Store
# Docker volumes
volumes/
data/
ใช้ Git Tags สำหรับ Version ของ Docker Images
วิธีที่ดีในการจัดการเวอร์ชันของ Docker Images คือการใช้ Git Tags ร่วมกับ Docker Image Tags ช่วยให้ติดตามเวอร์ชันได้ชัดเจน:
# สร้าง Git tag สำหรับ Release
git tag -a v1.0.0 -m "Release version 1.0.0"
git tag -a v1.1.0 -m "Release version 1.1.0"
git push origin --tags
# ดูเวอร์ชันปัจจุบัน
git describe --tags --always
# Output: v1.0.0-10-g7c9b8e2
# Build Docker image ด้วยเวอร์ชัน Git tag
VERSION=$(git describe --tags --always)
docker build -t myapp:$VERSION .
CI/CD Pipeline ด้วย Git และ Docker
สามารถสร้าง Automated CI/CD Pipeline ที่จะ Build และ Push Docker Image โดยอัตโนมัติเมื่อมี Commit ใหม่ โดยใช้ GitHub Actions หรือ GitLab CI:
# .github/workflows/docker-build.yml
name: Build and Push Docker Image
on:
push:
tags:
- 'v*'
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: docker/build-push-action@v2
with:
push: true
tags: myrepo/myapp:${{ github.ref_name }}
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
Multi-Stage Builds สำหรับ Dockerfile ที่มีประสิทธิภาพ
ใช้ Multi-stage Builds ใน Dockerfile เพื่อลดขนาด Final Image และเพิ่มความปลอดภัย:
# Multi-stage build สำหรับ Node.js Application
FROM node:16 AS builder
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
RUN npm run build
# Production stage
FROM node:16-alpine
WORKDIR /app
COPY --from=builder /app/dist ./dist
COPY --from=builder /app/node_modules ./node_modules
EXPOSE 3000
CMD ["node", "dist/server.js"]
Secrets Management – ห้ามเก็บ Secrets ใน Git
สิ่งสำคัญมากคือการไม่เก็บ Secrets เช่น Database Passwords หรือ API Keys ลงใน Git Repository การใช้ Docker Secrets หรือ Environment Variables ผ่าน docker-compose.yml คือวิธีที่ปลอดภัยกว่า:
# docker-compose.yml with secrets
version: '3.8'
services:
app:
image: myapp:latest
environment:
DB_PASSWORD_FILE: /run/secrets/db_password
secrets:
- db_password
db:
image: postgres:13
environment:
POSTGRES_PASSWORD_FILE: /run/secrets/db_password
secrets:
- db_password
secrets:
db_password:
file: ./secrets/db_password.txt
Branching Strategy สำหรับ Docker Projects
ให้ใช้ Git Branching Strategy ที่เหมาะสม เช่น Git Flow เพื่อจัดการ Development, Staging และ Production Environments:
- main/master branch – เก็บ Production Code และ Docker Images ที่เสถียร
- develop branch – สำหรับ Development ตรวจสอบและทดสอบ
- feature branches – สำหรับการเพิ่มฟีเจอร์ใหม่
- hotfix branches – สำหรับแก้ไข Bugs ด่วน
Best Practices สำหรับ Git และ Docker
- ห้ามเก็บไฟล์ .env และ Secrets ลงใน Git Repository
- ใช้ .gitignore เพื่อไม่ให้ Track ไฟล์ Build Artifacts
- ตรวจสอบ Dockerfile และ docker-compose.yml ด้วย Code Review ก่อน Merge
- ใช้ Git Tags สำหรับการจัดการเวอร์ชันของ Docker Images
- สร้าง Automated CI/CD Pipelines เพื่อ Build และ Deploy Docker Containers
- ใช้ Semantic Versioning (SemVer) สำหรับ Docker Image Tags
- เก็บ Docker Compose Configuration Files ที่ Production ไว้ใน Secrets Management
สรุป
Git และ Docker เมื่อทำงานร่วมกันอย่างมีประสิทธิภาพจะช่วยให้ทีมพัฒนาสามารถ Manage Version, Deploy Application และ Maintain Code ได้อย่างมีศศนา ระบบ CI/CD ที่ดีร่วมกับ Best Practices ในการใช้ Git และ Docker จะช่วยให้กระบวนการพัฒนาเป็นไปอย่างราบรื่น และลดข้อผิดพลาดในการ Deploy ทำให้แอปพลิเคชันของคุณทำงานได้มั่นคง
