From fda1b130ff09cb29c6319c5235348067af197ee7 Mon Sep 17 00:00:00 2001 From: mobiniqow Date: Wed, 16 Sep 2026 00:01:10 +0330 Subject: [PATCH] Add Docker support and docker-compose orchestration Multi-stage Dockerfile for the Go API, plus a docker-compose.yml that runs postgres + backend + both frontends together for local full-stack dev (frontends built from the sibling repo directories). Co-Authored-By: Claude Sonnet 5 --- .dockerignore | 10 ++++++++ .env.example | 8 ++++++ Dockerfile | 23 +++++++++++++++++ docker-compose.yml | 64 ++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 105 insertions(+) create mode 100644 .dockerignore create mode 100644 Dockerfile create mode 100644 docker-compose.yml diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..eac3203 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,10 @@ +.git +.idea +.claude +.env +.env.* +!.env.example +uploads +*.md +/dating-app +/dating-app-server diff --git a/.env.example b/.env.example index 3a65cc6..088a927 100644 --- a/.env.example +++ b/.env.example @@ -18,3 +18,11 @@ OTP_LENGTH=4 OTP_HASH_SECRET=test-otp-secret ADMIN_BOOTSTRAP_PHONE=09120000000 + +# -------- فقط برای docker-compose (نیازی نیست موقع اجرای مستقیم go run باشه) -------- +# DB_HOST رو docker-compose خودش موقع اجرای backend روی "postgres" ست می‌کنه؛ +# مقدار بالا (localhost) فقط برای اجرای مستقیم روی سیستم خودت استفاده می‌شه. +WEB_PORT=3000 +ADMIN_PORT=3001 +# آدرسی که از مرورگر کاربر قابل‌دسترسیه (نه آدرس داخلی شبکه‌ی داکر) +PUBLIC_API_URL=http://localhost:8080/api/v1 diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..111ffab --- /dev/null +++ b/Dockerfile @@ -0,0 +1,23 @@ +# ---- مرحله‌ی build ---- +FROM golang:1.22-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 /out/server ./cmd/api + +# ---- مرحله‌ی اجرا (ایمیج نهایی سبک) ---- +FROM alpine:3.20 +RUN apk add --no-cache ca-certificates tzdata +WORKDIR /app + +COPY --from=builder /out/server ./server + +# محل ذخیره‌ی عکس‌های آپلودشده؛ باید به یک volume متصل بشه تا با هر +# دیپلوی جدید پاک نشه (service.LocalPhotoStorage دقیقاً همین مسیر رو می‌سازه) +RUN mkdir -p /app/uploads/photos + +EXPOSE 8080 +ENTRYPOINT ["./server"] diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..07aee9e --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,64 @@ +name: dating-app + +services: + postgres: + image: postgres:16-alpine + restart: unless-stopped + environment: + POSTGRES_USER: ${DB_USER:-postgres} + POSTGRES_PASSWORD: ${DB_PASSWORD:-postgres} + POSTGRES_DB: ${DB_NAME:-dating_app} + volumes: + - postgres_data:/var/lib/postgresql/data + ports: + - "${DB_PORT:-5432}:5432" + healthcheck: + test: ["CMD-SHELL", "pg_isready -U ${DB_USER:-postgres} -d ${DB_NAME:-dating_app}"] + interval: 5s + timeout: 5s + retries: 10 + + backend: + build: + context: . + restart: unless-stopped + depends_on: + postgres: + condition: service_healthy + env_file: + - .env + environment: + # داخل شبکه‌ی داکر، بک‌اند باید با نام سرویس به دیتابیس وصل بشه نه localhost + DB_HOST: postgres + ports: + - "${APP_PORT:-8080}:8080" + volumes: + - backend_uploads:/app/uploads + + web: + build: + context: ../../../frontend/dating-app-web + args: + # این آدرس توی مرورگر کاربر استفاده می‌شه، پس باید از بیرون قابل‌دسترس باشه + # (نه آدرس داخلی شبکه‌ی داکر مثل http://backend:8080) + NEXT_PUBLIC_API_URL: ${PUBLIC_API_URL:-http://localhost:8080/api/v1} + restart: unless-stopped + depends_on: + - backend + ports: + - "${WEB_PORT:-3000}:3000" + + admin: + build: + context: ../../../frontend/dating-app-admin + args: + NEXT_PUBLIC_API_URL: ${PUBLIC_API_URL:-http://localhost:8080/api/v1} + restart: unless-stopped + depends_on: + - backend + ports: + - "${ADMIN_PORT:-3001}:3000" + +volumes: + postgres_data: + backend_uploads: