Fiber #

Dalam pengembangan aplikasi backend modern, Docker hampir menjadi standar de‑facto. Untuk aplikasi Go Fiber, Docker memberikan konsistensi environment, mempercepat onboarding developer, dan mengurangi problem klasik “works on my machine”.

Artikel ini akan membahas secara mendetail dan rinci:

  • Konsep Dockerfile untuk aplikasi Go Fiber
  • Struktur Dockerfile yang ideal untuk development lokal
  • Penggunaan Docker Compose untuk local development
  • Alur kerja (workflow) development Go Fiber dengan Docker
  • Best practice dan kesalahan umum yang sering terjadi

Fokus utama artikel ini adalah Docker Compose untuk local development, bukan production.

Gambaran Arsitektur #

Pada umumnya, setup local development Go Fiber dengan Docker terdiri dari:

  • Service API Go Fiber
  • (Opsional) Database seperti PostgreSQL / MySQL / Redis
  • Volume untuk live reload
  • Network internal antar container

Diagram sederhananya:

Developer
   |
   v
Docker Compose
   |
   +--> go-fiber-app (container)
   |
   +--> database (container)

Dasar Dockerfile Go Fiber #

Tujuan Dockerfile #

Untuk local development, Dockerfile tidak perlu terlalu agresif di optimasi size, berbeda dengan production. Prioritas utamanya:

  • Build cepat
  • Support hot reload
  • Mudah dibaca dan dipahami

Contoh Struktur Project #

.
├── cmd/
│   └── api/
│       └── main.go
├── internal/
├── go.mod
├── go.sum
├── Dockerfile
├── docker-compose.yml
└── .env

Dockerfile untuk Go Fiber #

Contoh Dockerfile #

FROM golang:1.22-alpine

WORKDIR /app

# Install tools tambahan (optional)
RUN apk add --no-cache git bash

# Copy dependency terlebih dahulu
COPY go.mod go.sum ./
RUN go mod download

# Copy seluruh source code
COPY . .

# Install air untuk hot reload
RUN go install github.com/cosmtrek/air@latest

EXPOSE 3000

CMD ["air", "-c", ".air.toml"]

Penjelasan Setiap Bagian #

1. Base Image #

FROM golang:1.22-alpine
  • alpine ringan dan cepat
  • Cocok untuk development
  • Sudah termasuk Go toolchain

2. Working Directory #

WORKDIR /app
  • Semua perintah berikutnya akan dijalankan di /app
  • Menghindari path yang berantakan

3. Install Tools Tambahan #

RUN apk add --no-cache git bash

Digunakan untuk:

  • Mengambil dependency private
  • Debugging manual di container

4. Copy Dependency Terlebih Dahulu #

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

Ini penting karena:

  • Layer cache Docker bisa dimanfaatkan
  • Build lebih cepat jika source code berubah tapi dependency tidak

5. Copy Source Code #

COPY . .
  • Seluruh source code disalin
  • Akan dioverride oleh volume di docker-compose (untuk live reload)

6. Hot Reload dengan Air #

RUN go install github.com/cosmtrek/air@latest

air adalah tool populer untuk:

  • Auto rebuild
  • Auto restart server Go

Sangat ideal untuk local development.

7. Expose Port #

EXPOSE 3000
  • Port internal container
  • Harus sesuai dengan port Fiber

8. Command Default #

CMD ["air", "-c", ".air.toml"]
  • Menjalankan aplikasi dengan hot reload

Docker Compose #

Tujuan Docker Compose #

Docker Compose digunakan untuk:

  • Menjalankan banyak service sekaligus
  • Mengatur dependency antar service
  • Menyederhanakan command Docker

Contoh docker-compose.yml #

version: "3.9"

services:
  api:
    container_name: go-fiber-api
    build:
      context: .
      dockerfile: Dockerfile
    ports:
      - "3000:3000"
    volumes:
      - .:/app
    env_file:
      - .env
    depends_on:
      - db
    command: air -c .air.toml

  db:
    image: postgres:16-alpine
    container_name: postgres-db
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: postgres
      POSTGRES_DB: app_db
    ports:
      - "5432:5432"
    volumes:
      - postgres_data:/var/lib/postgresql/data

volumes:
  postgres_data:

Penjelasan docker-compose.yml #

1. Version #

version: "3.9"

Versi stabil dan umum digunakan.

2. Service API (Go Fiber) #

Build Configuration #

build:
  context: .
  dockerfile: Dockerfile
  • Build image dari Dockerfile lokal

Port Mapping #

ports:
  - "3000:3000"
  • host:container
  • Akses via localhost:3000

Volume Mount (Live Reload) #

volumes:
  - .:/app

Ini kunci local development:

  • Source code lokal disinkronkan ke container
  • air mendeteksi perubahan file

Environment Variable #

env_file:
  - .env
  • Konfigurasi dipisahkan dari code
  • Lebih aman dan fleksibel

depends_on #

depends_on:
  - db
  • Container API akan dijalankan setelah DB
  • Tidak menjamin DB siap menerima koneksi (perlu retry di aplikasi)

3. Service Database #

image: postgres:16-alpine
  • Menggunakan image resmi
  • Alpine untuk ukuran kecil

4. Volume Database #

volumes:
  - postgres_data:/var/lib/postgresql/data
  • Data tetap aman walaupun container restart

Workflow Development #

  1. Jalankan:

    docker compose up --build
    
  2. Akses API:

    http://localhost:3000
    
  3. Edit code di lokal

  4. air otomatis rebuild dan restart


Kesalahan Umum #

  1. Tidak menggunakan volume → tidak ada live reload
  2. Menggunakan multi-stage build seperti production
  3. Hardcode env di Dockerfile
  4. Tidak menggunakan .dockerignore

Best Practice #

1. Pisahkan Dockerfile Dev dan Prod #

  • Dockerfile → development
  • Dockerfile.prod → production

2. Jangan Build Binary di Local Dev #

  • Gunakan go run atau air
  • Lebih cepat dan fleksibel

3. Gunakan .dockerignore #

Contoh:

.git
node_modules
vendor
*.log

4. Implement Retry Logic ke Database #

Karena depends_on tidak menjamin readiness.


Penutup #

Dockerfile dan Docker Compose untuk local development Go Fiber seharusnya fokus pada developer experience, bukan sekadar efisiensi image.

Dengan setup yang tepat:

  • Onboarding developer lebih cepat
  • Debugging lebih mudah
  • Konsistensi environment terjaga
About | Author | Content Scope | Editorial Policy | Privacy Policy | Disclaimer | Contact