From 862e8ca8460d4a0dd0f0c183a501ece2aed0ef62 Mon Sep 17 00:00:00 2001 From: Ali Alavi Date: Thu, 12 Nov 2026 08:07:53 +0000 Subject: [PATCH] feat(staging): add dockerfile, staging compose, and nginx internal proxy for habib shop --- .dockerignore | 7 ++++ .env.staging | 4 ++ Dockerfile | 37 ++++++++++++++++++ docker-compose.staging.yml | 31 +++++++++++++++ nginx.staging.conf | 77 ++++++++++++++++++++++++++++++++++++++ 5 files changed, 156 insertions(+) create mode 100644 .dockerignore create mode 100644 .env.staging create mode 100644 Dockerfile create mode 100644 docker-compose.staging.yml create mode 100644 nginx.staging.conf diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..33b10e4 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,7 @@ +node_modules +dist +.git +.gitignore +*.log +README.md +BACKEND_SPEC.md diff --git a/.env.staging b/.env.staging new file mode 100644 index 0000000..fe4f015 --- /dev/null +++ b/.env.staging @@ -0,0 +1,4 @@ +VITE_USE_MOCK_DATA=false +VITE_API_BASE_URL=/api/shop +VITE_MEDIA_ORIGIN= +VITE_BACKEND_ORIGIN=http://najm_staging_web:8000 diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..b540ef8 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,37 @@ +# syntax=docker/dockerfile:1.7 + +FROM node:22-alpine AS deps +RUN apk add --no-cache libc6-compat +WORKDIR /app + +COPY package.json package-lock.json* ./ +RUN npm install + +FROM node:22-alpine AS builder +WORKDIR /app + +COPY --from=deps /app/node_modules ./node_modules +COPY . . + +ARG VITE_USE_MOCK_DATA=false +ARG VITE_API_BASE_URL=/api/shop +ARG VITE_MEDIA_ORIGIN= +ENV VITE_USE_MOCK_DATA=$VITE_USE_MOCK_DATA +ENV VITE_API_BASE_URL=$VITE_API_BASE_URL +ENV VITE_MEDIA_ORIGIN=$VITE_MEDIA_ORIGIN +ENV NODE_ENV=production + +RUN npm run build + +FROM nginx:1.27-alpine AS runner +RUN apk add --no-cache wget + +COPY nginx.staging.conf /etc/nginx/conf.d/default.conf +COPY --from=builder /app/dist /usr/share/nginx/html + +EXPOSE 3000 + +HEALTHCHECK --interval=30s --timeout=5s --start-period=10s --retries=3 \ + CMD wget -qO- http://127.0.0.1:3000/healthz || exit 1 + +CMD ["nginx", "-g", "daemon off;"] diff --git a/docker-compose.staging.yml b/docker-compose.staging.yml new file mode 100644 index 0000000..76dcdfe --- /dev/null +++ b/docker-compose.staging.yml @@ -0,0 +1,31 @@ +services: + web: + build: + context: . + dockerfile: Dockerfile + args: + VITE_USE_MOCK_DATA: "false" + VITE_API_BASE_URL: "/api/shop" + VITE_MEDIA_ORIGIN: "" + image: shop_staging_web:latest + container_name: shop_staging_web + restart: unless-stopped + env_file: + - .env.staging + ports: + - "127.0.0.1:5562:3000" + environment: + - NODE_ENV=production + - API_BASE_URL=http://najm_staging_web:8000 + healthcheck: + test: ["CMD", "wget", "-qO-", "http://127.0.0.1:3000/healthz"] + interval: 30s + timeout: 5s + retries: 3 + start_period: 10s + networks: + - najm_staging_net + +networks: + najm_staging_net: + external: true diff --git a/nginx.staging.conf b/nginx.staging.conf new file mode 100644 index 0000000..4163fd0 --- /dev/null +++ b/nginx.staging.conf @@ -0,0 +1,77 @@ +server { + listen 3000; + server_name _; + + root /usr/share/nginx/html; + index index.html; + + client_max_body_size 100M; + + # Docker internal DNS resolver so upstream container IPs re-resolve dynamically + resolver 127.0.0.11 valid=10s ipv6=off; + set $backend_upstream http://najm_staging_web:8000; + + gzip on; + gzip_vary on; + gzip_proxied any; + gzip_comp_level 6; + gzip_types text/plain text/css text/xml application/json application/javascript image/svg+xml; + + location = /healthz { + access_log off; + default_type application/json; + return 200 '{"status":"ok","service":"habib-shop-staging"}'; + } + + location /api/shop/ { + proxy_pass $backend_upstream; + proxy_http_version 1.1; + proxy_set_header Host habib.nwhco.ir; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-Forwarded-Proto $scheme; + proxy_read_timeout 300s; + } + + location /upload-tmp-media/ { + proxy_pass $backend_upstream; + proxy_http_version 1.1; + proxy_set_header Host habib.nwhco.ir; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-Forwarded-Proto $scheme; + proxy_read_timeout 300s; + } + + location /habcoin/ { + proxy_pass $backend_upstream; + proxy_http_version 1.1; + proxy_set_header Host habib.nwhco.ir; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-Forwarded-Proto $scheme; + proxy_read_timeout 300s; + } + + location /media/ { + proxy_pass $backend_upstream; + proxy_http_version 1.1; + proxy_set_header Host habib.nwhco.ir; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-Forwarded-Proto $scheme; + expires 30d; + add_header Cache-Control "public, immutable"; + } + + location /assets/ { + try_files $uri =404; + expires 30d; + add_header Cache-Control "public, max-age=2592000, immutable"; + } + + location / { + try_files $uri $uri/ /index.html; + add_header Cache-Control "no-cache"; + } +}