Dockerfile Multistage Anti-Bloat Biar Image Prod Nggak Mencret

212 words • 1,410 chars Markdown
Raw

Dockerfile Multistage Anti-Bloat Biar Image Prod Nggak Mencret

Yo, night owls dan para penjaga server prod yang masih melek jam 2 pagi. Capek kan image Docker lu pada bengkak kayak bangkai paus gara-gara males misahin build tools? Giliran di-deploy, makan kuota VPS mulu.

Nih, kasih paham pake Dockerfile multistage build. Biar yang masuk ke production cuma binary atau file mateng doang, compiler dan sampah-sampahnya dibuang ke tong sampah digital.

Snippet Dockerfile Multistage (Golang/Node Style)

# Stage 1: Build the artifact
FROM golang:1.21-alpine AS builder
WORKDIR /app

COPY go.mod go.sum ./
RUN go mod download

COPY . .
RUN CGO_ENABLED=0 GOOS=linux go build -ldflags="-s -w" -o myapp .

# Stage 2: Run the production container
FROM alpine:3.19
WORKDIR /root/

# Install ca-certificates buat HTTPS (kalau butuh)
RUN apk --no-cache add ca-certificates

# Comot hasil compile dari builder stage, abaikan source code!
COPY --from=builder /app/myapp .

EXPOSE 8080
CMD ["./myapp"]

Kenapa Harus Begini?

  1. Image Ramping: Nggak perlu bawa-bawa go SDK atau node_modules segaban ke server prod. Aman dari mata-mata hacker yang suka nyari celah unused packages.
  2. Deploy Ngebut: Pull image dari registry pas auto-deploy bakal sat-set, gak bikin CI/CD lu timeout.
  3. Tidur Nyenyak: Server anteng, boss seneng, lu bisa lanjut ngopi.

Udah, salin sono. Jangan lupa prune docker lu, itu disk root / udah mau penuh, woi!