diff --git a/Dockerfile b/Dockerfile index c8caadd..1dd4646 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM node:20-bullseye-slim +FROM node:20-alpine # Install Git during the build phase using apt. RUN apt-get update -y && apt-get install -y git && rm -rf /var/lib/apt/lists/* diff --git a/docker-compose.yaml b/docker-compose.yaml index 2986ffe..bb4059d 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -1,31 +1,99 @@ -version: "3.8" +version: "3.9" + services: - medusa: + # ─────────────────────── Databases & Caches ─────────────────────── + postgres: + image: postgres:16-alpine + restart: unless-stopped + environment: + POSTGRES_USER: postgres + POSTGRES_PASSWORD: ${POSTGRES_PASSWORD} + POSTGRES_DB: postgres + volumes: + - postgres-data:/var/lib/postgresql/data + networks: [backend] + + redis: + image: redis:7-alpine + restart: unless-stopped + command: ["redis-server", "--requirepass", "${REDIS_PASSWORD}"] + volumes: + - redis-data:/data + networks: [backend] + + minio: + image: minio/minio:RELEASE.2024-04-18T19-09-19Z + restart: unless-stopped + environment: + MINIO_ROOT_USER: ${MINIO_ROOT_USER} + MINIO_ROOT_PASSWORD: ${MINIO_ROOT_PASSWORD} + command: server /data --console-address ":9001" + volumes: + - minio-data:/data + ports: # expose console if you like; remove ons‑prod + - "9001:9001" + networks: [backend] + + # ─────────────────────── Medusa processes ─────────────────────── + medusa-server: build: context: . - dockerfile: Dockerfile + dockerfile: server/Server.Dockerfile # path relative to repo root + depends_on: [postgres, redis, minio] + restart: unless-stopped + environment: + # Core DB / cache + DATABASE_URL: postgres://postgres:${POSTGRES_PASSWORD}@postgres:5432/postgres?sslmode=disable + REDIS_URL: redis://default:${REDIS_PASSWORD}@redis:6379/0 + # S3 (MinIO) storage + S3_ENDPOINT: http://minio:9000 + S3_REGION: ${S3_REGION:-us-east-1} + S3_BUCKET: ${S3_BUCKET} + S3_ACCESS_KEY_ID: ${MINIO_ROOT_USER} + S3_SECRET_ACCESS_KEY: ${MINIO_ROOT_PASSWORD} + # Medusa‑specific + MEDUSA_BACKEND_URL: ${MEDUSA_BACKEND_URL:-http://localhost:9000} + MEDUSA_WORKER_MODE: server + DISABLE_MEDUSA_ADMIN: "false" + COOKIE_SECRET: ${COOKIE_SECRET} + JWT_SECRET: ${JWT_SECRET} + # CORS etc. + ADMIN_CORS: ${ADMIN_CORS} + STORE_CORS: ${STORE_CORS} + AUTH_CORS: ${AUTH_CORS} + # Any other keys (Stripe, Resend, …) + STRIPE_API_KEY: ${STRIPE_API_KEY} + STRIPE_WEBHOOK_SECRET: ${STRIPE_WEBHOOK_SECRET} + RESEND_API_KEY: ${RESEND_API_KEY} + RESEND_FROM_EMAIL: ${RESEND_FROM_EMAIL} ports: - - "9000:9000" - environment: - # Default environment variables - DATABASE_URL: "postgres://medusa:medusa@db:5432/medusa?ssl_mode=disable" - # You can include additional environment variables as needed, e.g.: - MEDUSA_STORE_NAME: "My Medusa Store" - MEDUSA_ADMIN_EMAIL: "admin@medusajs.com" - MEDUSA_ADMIN_PASSWORD: "supersecret" - volumes: - # Persist the generated Medusa project so that installation only runs once. - - medusa_app_data:/app + - "9000:9000" # expose externally + networks: [backend] - db: - image: postgres:13 + medusa-worker: + build: + context: . + dockerfile: server/Worker.Dockerfile + depends_on: [postgres, redis] + restart: unless-stopped environment: - POSTGRES_USER: medusa - POSTGRES_PASSWORD: medusa - POSTGRES_DB: medusa - volumes: - - postgres_data:/var/lib/postgresql/data + DATABASE_URL: postgres://postgres:${POSTGRES_PASSWORD}@postgres:5432/postgres?sslmode=disable + REDIS_URL: redis://default:${REDIS_PASSWORD}@redis:6379/0 + S3_ENDPOINT: http://minio:9000 + S3_REGION: ${S3_REGION:-us-east-1} + S3_BUCKET: ${S3_BUCKET} + S3_ACCESS_KEY_ID: ${MINIO_ROOT_USER} + S3_SECRET_ACCESS_KEY: ${MINIO_ROOT_PASSWORD} + MEDUSA_WORKER_MODE: worker + DISABLE_MEDUSA_ADMIN: "true" + networks: [backend] +# ────────────────────────── Shared Stuff ────────────────────────── volumes: - medusa_app_data: - postgres_data: + postgres-data: + redis-data: + minio-data: + +networks: + backend: + driver: bridge diff --git a/server/Server.Dockerfile b/server/Server.Dockerfile new file mode 100644 index 0000000..63f02f8 --- /dev/null +++ b/server/Server.Dockerfile @@ -0,0 +1,41 @@ +# Use Node.js base image +FROM node:20-alpine +LABEL authors="clait" + +# Install dependencies only once +RUN apk add --no-cache curl && corepack enable + +# Set working directory +WORKDIR /usr/src/app + +# Copy dependency files first to leverage Docker caching +COPY package.json yarn.lock* package-lock.json* pnpm-lock.yaml* ./ + +# Install dependencies based on the lock file available +RUN \ + if [ -f yarn.lock ]; then yarn install --immutable; \ + elif [ -f package-lock.json ]; then npm ci; \ + elif [ -f pnpm-lock.yaml ]; then pnpm i --frozen-lockfile; \ + else echo "Lockfile not found." && exit 1; \ + fi + +# Copy the rest of the application files (to avoid re-triggering dependency install step unnecessarily) +COPY . . + +# Build the Medusa application +RUN \ + if [ -f yarn.lock ]; then yarn build; \ + elif [ -f package-lock.json ]; then npm run build; \ + elif [ -f pnpm-lock.yaml ]; then pnpm run build; \ + else echo "Lockfile not found." && exit 1; \ + fi + +# Install Medusa dependencies +WORKDIR /usr/src/app/.medusa/server +RUN npm install + +# Expose default Medusa port +EXPOSE 9000 + +# Command to run in production +CMD ["sh", "-c", "npm run predeploy && npm run start"] \ No newline at end of file diff --git a/server/Worker.Dockerfile b/server/Worker.Dockerfile new file mode 100644 index 0000000..59a4595 --- /dev/null +++ b/server/Worker.Dockerfile @@ -0,0 +1,41 @@ +# Use Node.js base image +FROM node:20-alpine +LABEL authors="clait" + +# Install dependencies only once +RUN apk add --no-cache curl && corepack enable + +# Set working directory +WORKDIR /usr/src/app + +# Copy dependency files first to leverage Docker caching +COPY package.json yarn.lock* package-lock.json* pnpm-lock.yaml* ./ + +# Install dependencies based on the lock file available +RUN \ + if [ -f yarn.lock ]; then yarn install --immutable; \ + elif [ -f package-lock.json ]; then npm ci; \ + elif [ -f pnpm-lock.yaml ]; then pnpm i --frozen-lockfile; \ + else echo "Lockfile not found." && exit 1; \ + fi + +# Copy the rest of the application files (to avoid re-triggering dependency install step unnecessarily) +COPY . . + +# Build the Medusa application +RUN \ + if [ -f yarn.lock ]; then yarn build; \ + elif [ -f package-lock.json ]; then npm run build; \ + elif [ -f pnpm-lock.yaml ]; then pnpm run build; \ + else echo "Lockfile not found." && exit 1; \ + fi + +# Install Medusa dependencies +WORKDIR /usr/src/app/.medusa/server +RUN npm install + +# Expose default Medusa port +EXPOSE 9000 + +# Command to run in production +CMD ["sh", "-c", "npm run start"] \ No newline at end of file