🏠 Home
Slide 1 / 12

🐳 Dockerfile

Cara Build Image yang Benar

Learn by Doing

Apa itu Dockerfile?

Dockerfile adalah file teks yang berisi instruksi untuk membangun Docker image. Seperti resep:

  • Base OS β†’ dari image mana memulai
  • Dependencies β†’ paket apa saja perlu install
  • Files β†’ file apa perlu di-copy
  • Commands β†’ perintah apa perlu dijalankan

Hasilnya: Image yang bisa dijalankan sebagai Container

Struktur Dasar

# Comment
FROM ubuntu:24.04              # Base image (wajib)
LABEL maintainer="[email protected]"  # Metadata
ENV VAR_NAME=value            # Environment variables
WORKDIR /app                  # Working directory
COPY . /app                   # Copy files
RUN command                   # Execute saat build
EXPOSE 8080                   # Dokumentasi port
ENTRYPOINT ["cmd"]            # Entry point
CMD ["arg"]                   # Default arguments

FROM - Base Image

Setiap Dockerfile harus dimulai dengan FROM. Ini adalah fondasi image kamu.

Contoh:

FROM ubuntu:24.04          # Ubuntu LTS
FROM python:3.11-slim     # Python + slim OS
FROM node:20-alpine      # Node.js + Alpine (paling kecil)
FROM nginx:alpine         # Nginx web server
FROM postgres:16         # PostgreSQL database
Tip: Gunakan tag spesifik, jangan :latest

RUN - Eksekusi Commands

RUN menjalankan perintah saat proses build image.

Contoh:

# Install packages
RUN apt-get update && apt-get install -y nginx

# Install Python packages
RUN pip install flask gunicorn

# Install Node packages globally
RUN npm install -g pm2

# Create directory
RUN mkdir -p /app/logs
Best Practice: Gabungkan beberapa RUN untuk kurangi layer

COPY - Copy Files

Menyalin file dari build context ke dalam image.

Contoh:

COPY source destination

COPY package*.json /app/      # Copy file matching pattern
COPY requirements.txt .       # Current dir ke workdir
COPY . /app/                  # Copy semua file
Penting: Selalu tambahkan .dockerignore untuk exclude file yang tidak perlu

Instruksi Lainnya

WORKDIR - Working Directory

WORKDIR /app
WORKDIR /var/www/html

ENV - Environment Variables

ENV NODE_ENV=production
ENV PORT=3000

EXPOSE - Dokumentasi Port

EXPOSE 80
EXPOSE 3000 8080

CMD vs ENTRYPOINT

Instruction Purpose Use Case
CMD Default command, bisa di-override Main process
ENTRYPOINT Fixed entry point, args di-append Always executable

Contoh:

# CMD - bisa dioverride
CMD ["python", "app.py"]
# docker run image custom.py  <- runs custom.py

# ENTRYPOINT - appends args
ENTRYPOINT ["python", "app.py"]
# docker run image --debug  <- runs app.py --debug

Multi-Stage Builds

Kurangi ukuran image dengan menggunakan beberapa stage.

# Stage 1: Build
FROM node:20-alpine AS builder
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
RUN npm run build

# Stage 2: Production
FROM node:20-alpine
WORKDIR /app
COPY --from=builder /app/dist ./dist
CMD ["node", "dist/index.js"]
Keuntungan: Image final tidakεŒ…ε« build tools, lebih kecil & aman

.dockerignore

Exclude file yang tidak perlu dari build context.

.git
node_modules
*.log
.env
.DS_Store
__pycache__
*.md
vendor/
dist/
build/
*.tar.gz
Wajib! Selalu ada .dockerignore agar tidak salah kirim file sensitif

Best Practices

  1. Gunakan tag spesifik - Jangan :latest
  2. Use slim/alpine images - Ukuran lebih kecil
  3. Order by change frequency - Item yang jarang berubah taruh pertama (cache optimization)
  4. Gunakan .dockerignore - Exclude file tidak perlu
  5. Run as non-root user - Keamanan
  6. Multi-stage builds - Image final lebih kecil
  7. Gabungkan RUN commands - Kurangi layers

Build & Run Commands

Build Image:

docker build -t myapp:latest .
docker build -t myapp:1.0 .
docker build -f Dockerfile.prod -t myapp:prod .

Run Container:

docker run -d -p 8080:80 myapp:latest
docker run -d -p 8080:80 -e NODE_ENV=production myapp:latest
docker run -d -p 8080:80 -v $(pwd):/app myapp:latest

Useful Flags: