diff --git a/.gitignore b/.gitignore index f4fffc7..962bd65 100644 --- a/.gitignore +++ b/.gitignore @@ -1,15 +1,16 @@ # Environment files .env .env.local -.env.production +.env.prod # Docker -docker-compose.override.yml +docker-compose.local.yml # Database data mysql-data/ nordicstorium-mysql-data/ +Caddyfile # Node node_modules/ npm-debug.log* diff --git a/Dockerfile.prod b/Dockerfile.prod new file mode 100644 index 0000000..06202c9 --- /dev/null +++ b/Dockerfile.prod @@ -0,0 +1,58 @@ +FROM node:20-alpine AS base + +# Install dependencies only when needed +FROM base AS deps +RUN apk add --no-cache libc6-compat +WORKDIR /app + +COPY package.json package-lock.json* ./ +RUN npm ci + +# Rebuild the source code only when needed +FROM base AS builder +WORKDIR /app +COPY --from=deps /app/node_modules ./node_modules +COPY . . + +# --- ADD THIS SECTION --- +# Accept the public variables as build arguments +ARG NEXT_PUBLIC_APP_URL +ARG NEXT_PUBLIC_API_URL + +# Make them available to the build command +ENV NEXT_PUBLIC_APP_URL=$NEXT_PUBLIC_APP_URL +ENV NEXT_PUBLIC_API_URL=$NEXT_PUBLIC_API_URL +# ------------------------ + +ENV NEXT_TELEMETRY_DISABLED=1 + +RUN npm run build + +# Production image, copy all the files and run next +FROM base AS runner +WORKDIR /app + +ENV NODE_ENV=production +ENV NEXT_TELEMETRY_DISABLED=1 + +RUN addgroup --system --gid 1001 nodejs +RUN adduser --system --uid 1001 nextjs + +COPY --from=builder /app/public ./public + +# Set the correct permission for prerender cache +mkdir .next +chown nextjs:nodejs .next + +# Automatically leverage output traces to reduce image size +COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./ +COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static + +USER nextjs + +EXPOSE 3000 + +ENV PORT=3000 +ENV HOSTNAME="0.0.0.0" + +CMD ["node", "server.js"] \ No newline at end of file diff --git a/docker-compose.prod.yml b/docker-compose.prod.yml new file mode 100644 index 0000000..70daa52 --- /dev/null +++ b/docker-compose.prod.yml @@ -0,0 +1,73 @@ +version: '3.8' + +services: + # Next.js Application + app: + container_name: nordicstorium-app + build: + context: . + dockerfile: Dockerfile.prod + args: + NEXT_PUBLIC_APP_URL: "https://store.abrahem.se" + NEXT_PUBLIC_API_URL: "https://store.abrahem.se/api" + restart: always + ports: + - "3005:3000" + environment: + - DATABASE_URL=mysql://${DB_USER}:${DB_PASSWORD}@db:3306/${DB_NAME} + - JWT_SECRET=${JWT_SECRET} + - NODE_ENV=production + - HOSTNAME=0.0.0.0 + - SMTP_HOST=${SMTP_HOST} + - SMTP_PORT=${SMTP_PORT} + - SMTP_SECURE=${SMTP_SECURE} + - SMTP_USER=${SMTP_USER} + - SMTP_PASS=${SMTP_PASS} + - SMTP_FROM=${SMTP_FROM} + - SMTP_FROM_NAME=${SMTP_FROM_NAME} + - CONTACT_EMAIL=${CONTACT_EMAIL} + - GOOGLE_CLIENT_ID=${GOOGLE_CLIENT_ID} + - GOOGLE_CLIENT_SECRET=${GOOGLE_CLIENT_SECRET} + - GOOGLE_REDIRECT_URI=https://store.abrahem.se/api/auth/google/callback + depends_on: + - db + networks: + - nordic_net + + # MariaDB Database (for App) + db: + container_name: nordicstorium-db + image: mariadb:10.11 + restart: always + environment: + MARIADB_DATABASE: ${DB_NAME} + MARIADB_USER: ${DB_USER} + MARIADB_PASSWORD: ${DB_PASSWORD} + MARIADB_ROOT_PASSWORD: ${DB_ROOT_PASSWORD} + volumes: + - mysql_data:/var/lib/mysql + networks: + - nordic_net + + # PhpMyAdmin (Database GUI) + phpmyadmin: + container_name: nordicstorium-phpmyadmin + image: phpmyadmin/phpmyadmin:latest + restart: always + ports: + - "8081:80" + environment: + PMA_HOST: db + PMA_PORT: 3306 + PMA_ARBITRARY: 1 + networks: + - nordic_net + depends_on: + - db + +volumes: + mysql_data: + +networks: + nordic_net: + driver: bridge diff --git a/next.config.ts b/next.config.ts index d94f239..3eddc0d 100644 --- a/next.config.ts +++ b/next.config.ts @@ -1,6 +1,7 @@ import type { NextConfig } from "next"; const nextConfig: NextConfig = { + output: 'standalone', images: { remotePatterns: [ {