Aplikasi kompresi gambar berbasis Next.js (frontend) dan FastAPI (backend). Dikemas dengan Docker Compose untuk kemudahan menjalankan secara lokal.
Prasyarat:
- Docker & Docker Compose terpasang
Jalankan stack:
docker compose up -d --buildAkses:
- Frontend: http://localhost:3001/
- Backend: http://localhost:8001/ (Dokumentasi: http://localhost:8001/docs)
Hentikan:
docker compose downStatus & log:
docker compose ps
docker compose logs -f backend frontendNEXT_PUBLIC_API_URL(frontend)- Diset di
docker-compose.ymlkehttp://localhost:8001(build arg + env var). - Digunakan oleh browser saat memanggil API backend.
- Diset di
- CORS (backend)
- Sudah mengizinkan asal
http://localhost:3000danhttp://localhost:3001untuk pengembangan. - Ubah di
backend/main.pybila perlu.
- Sudah mengizinkan asal
- Kedua layanan memiliki healthcheck:
- Backend:
curl -fsS http://localhost:8001/ - Frontend:
curl -fsS http://localhost:3000/
- Backend:
- Kebijakan restart:
restart: unless-stopped(otomatis hidup kembali bila crash). - Frontend menunggu backend sehat via
depends_on(condition:service_healthy).
GET /— Health- Respons:
{ "status": "ok" }
- Respons:
POST /compress— Kompres gambar- Form fields:
image(file, wajib)quality(int, default 85, rentang 1–95)max_size(int, default 1920)progressive(bool, default true)
- Contoh:
- Form fields:
curl -X POST \
-F "image=@path/to/your.jpg" \
-F "quality=85" \
-F "max_size=1920" \
-F "progressive=true" \
http://localhost:8001/compress \
--output compressed.jpgFrontend (Next.js):
cd frontend
npm install
npm run dev
# buka http://localhost:3000Backend (FastAPI):
cd backend
uvicorn main:app --reload --host 0.0.0.0 --port 8001Catatan:
- Bila backend dev berjalan di 8001, hentikan container backend agar port tidak bentrok.
- CORS sudah mengizinkan
localhost:3000danlocalhost:3001untuk pengembangan.
image-compressor/
├── backend/ # FastAPI app (Dockerfile, main.py, requirements.txt)
├── frontend/ # Next.js app (Dockerfile, app/, components/ui/, lib/)
└── docker-compose.yml # Orkestrasi layanan
- Port 3000 di host terpakai:
- Frontend dipetakan ke
3001:3000, akses UI dihttp://localhost:3001/.
- Frontend dipetakan ke
- CORS error:
- Pastikan asal (origin) sesuai dan variabel
NEXT_PUBLIC_API_URLmengarah ke backend.
- Pastikan asal (origin) sesuai dan variabel
- Healthcheck gagal:
- Cek logs:
docker compose logs -f backend frontend.
- Cek logs:
- Pull image secara manual:
docker pull ghcr.io/setiawand/squeezer-backend:latestdocker pull ghcr.io/setiawand/squeezer-frontend:latest
- Jalankan lewat Compose tanpa build:
docker compose pull(menarik image terbaru dari GHCR)docker compose up -d(menjalankan container memakai image yang sudah dipull)
- Pin versi spesifik di Compose:
- Edit
docker-compose.ymluntuk menetapkan tag tertentu:
- Edit
services:
backend:
image: ghcr.io/setiawand/squeezer-backend:v1.0.0
frontend:
image: ghcr.io/setiawand/squeezer-frontend:v1.0.0
-
Menjalankan langsung dengan Docker (tanpa Compose):
- Backend:
docker run --rm -p 8001:8001 ghcr.io/setiawand/squeezer-backend:v1.0.0 - Frontend:
docker run --rm -p 3001:3000 ghcr.io/setiawand/squeezer-frontend:v1.0.0
- Backend:
-
Mengubah API URL untuk frontend:
- Penting:
NEXT_PUBLIC_API_URLdi-embed saat build Next.js. Jika backend Anda bukanhttp://localhost:8001, bangun image frontend baru dengan argumen build yang sesuai:
- Penting:
docker build -t ghcr.io/setiawand/squeezer-frontend:custom \
--build-arg NEXT_PUBLIC_API_URL=https://api.example.com \
-f frontend/Dockerfile frontend
- Alternatif via Compose (build dari sumber):
services:
frontend:
build:
context: ./frontend
args:
NEXT_PUBLIC_API_URL: https://api.example.com
image: ghcr.io/setiawand/squeezer-frontend:custom
- Autentikasi ke GHCR (bila paket private):
echo <PAT> | docker login ghcr.io -u setiawand --password-stdin- Minimal scope PAT untuk pull:
read:packages - Paket dapat dilihat di: https://github.com/users/setiawand/packages
Selamat menggunakan Squeezer!