Paket 2 · Praktik LanjutanModul B — CI/CD & Container Pipeline

Docker Image Build & Container Registry

Membangun image produksi, memberi tag yang tepat, push ke registry, serta mengelola cache, keamanan, dan retensi image.

Image adalah artefak deployment utama. Pipeline harus menghasilkan image yang kecil, aman, reproducible, dan dapat dilacak ke commit asal.

Build image

docker build -t app:local .
docker run --rm -p 3000:3000 app:local

Untuk produksi gunakan multi-stage dan base image minimal:

FROM node:22-alpine AS build
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
RUN npm run build

FROM node:22-alpine
WORKDIR /app
ENV NODE_ENV=production
COPY package*.json ./
RUN npm ci --omit=dev
COPY --from=build /app/dist ./dist
USER node
CMD ["node", "dist/server.js"]

Strategi tag

TagKegunaan
app:git-<sha>Unik per commit, paling aman untuk deploy
app:v1.4.0Versi rilis
app:stagingPenunjuk environment
app:latestKenyamanan, jangan untuk rollback

Contoh:

SHA=$(git rev-parse --short HEAD)
docker build -t ghcr.io/org/app:git-$SHA -t ghcr.io/org/app:latest .

Immutable tag

Untuk produksi, deploy memakai tag immutable seperti SHA atau versi. Tag mutable seperti latest membuat rollback tidak jelas.

Registry populer

RegistryCatatan
Docker HubUmum, rate limit untuk anonim
GHCRTerintegrasi GitHub
GitLab RegistryTerintegrasi project GitLab
HarborSelf-hosted, fitur scanning dan RBAC
AWS ECR/GCR/ACRTerintegrasi cloud masing-masing

Login dan push

GHCR

echo "$GITHUB_TOKEN" | docker login ghcr.io -u "$GITHUB_USER" --password-stdin
docker tag app:local ghcr.io/org/app:git-$SHA
docker push ghcr.io/org/app:git-$SHA

GitLab

echo "$CI_JOB_TOKEN" | docker login registry.gitlab.com -u "$CI_REGISTRY_USER" --password-stdin
docker tag app:local "$CI_REGISTRY_IMAGE:$CI_COMMIT_SHA"
docker push "$CI_REGISTRY_IMAGE:$CI_COMMIT_SHA"

Build cache

BuildKit inline cache

docker buildx build \
  --cache-from type=registry,ref=ghcr.io/org/app:buildcache \
  --cache-to type=registry,ref=ghcr.io/org/app:buildcache,mode=max \
  -t ghcr.io/org/app:git-$SHA \
  --push .

GitHub Actions cache

- uses: docker/setup-buildx-action@v3
- uses: docker/build-push-action@v6
  with:
    context: .
    push: true
    tags: ghcr.io/${{ github.repository }}:${{ github.sha }}
    cache-from: type=gha
    cache-to: type=gha,mode=max

Cache mempercepat build, terutama untuk layer dependensi.

Multi-architecture

docker buildx create --use
docker buildx build \
  --platform linux/amd64,linux/arm64 \
  -t ghcr.io/org/app:git-$SHA \
  --push .

Berguna bila server dan laptop memakai arsitektur berbeda.

Keamanan image

  • Gunakan base image minimal (alpine, distroless).
  • Jalankan sebagai user non-root.
  • Scan image sebelum push.
  • Jangan simpan secret di image.
  • Pin base image ke digest untuk reproducibility.
  • Perbarui base image secara rutin.
trivy image ghcr.io/org/app:git-$SHA

Retensi dan pembersihan

Registry bisa penuh jika semua image disimpan selamanya.

  • Simpan N versi terakhir.
  • Hapus tag sementara.
  • Pertahankan image yang sedang dipakai produksi.
  • Dokumentasikan kebijakan retensi.

Contoh aturan: simpan 10 image terakhir dan semua tag rilis.

Praktik: pipeline build & push

jobs:
  image:
    runs-on: ubuntu-latest
    permissions:
      contents: read
      packages: write
    steps:
      - uses: actions/checkout@v4
      - uses: docker/setup-buildx-action@v3
      - uses: docker/login-action@v3
        with:
          registry: ghcr.io
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}
      - uses: docker/build-push-action@v6
        with:
          context: .
          push: true
          tags: |
            ghcr.io/${{ github.repository }}:git-${{ github.sha }}
            ghcr.io/${{ github.repository }}:latest
          cache-from: type=gha
          cache-to: type=gha,mode=max

Ringkasan

  • Build image sekali dan tag dengan SHA serta versi.
  • Gunakan multi-stage, user non-root, dan base image minimal.
  • Registry menyimpan artefak untuk deployment.
  • Cache, scan, dan retensi menjaga kecepatan, keamanan, dan biaya.

Selanjutnya

Rangkai semuanya di Full CI/CD Pipeline.

On this page