content: FROM node:18-alpine AS base
  
  # Install dependencies only when needed
  FROM base AS deps
  RUN apk add --no-cache libc6-compat
  WORKDIR /app
  
  COPY ./frontend/web/package.json ./frontend/web/package-lock.json ./
  RUN \
    if [ -f yarn.lock ]; then yarn --frozen-lockfile; \
    elif [ -f package-lock.json ]; then npm ci; \
    elif [ -f pnpm-lock.yaml ]; then corepack enable pnpm && pnpm i --frozen-lockfile; \
    else echo "Lockfile not found." && exit 1; \
    fi
  
  # Rebuild the source code only when needed
  FROM base AS builder
  WORKDIR /app
  COPY --from=deps /app/node_modules ./node_modules
  COPY ./frontend/web/ ./
  RUN \
    if [ -f yarn.lock ]; then yarn run build; \
    elif [ -f package-lock.json ]; then npm run build; \
    elif [ -f pnpm-lock.yaml ]; then corepack enable pnpm && pnpm run build; \
    else echo "Lockfile not found." && exit 1; \
    fi
  
  # Production image, copy all the files and run next
  FROM base AS runner
  WORKDIR /app
  
  ENV NODE_ENV=production
  
  RUN addgroup --system --gid 1001 nodejs
  RUN adduser --system --uid 1001 nextjs
  
  COPY --from=builder /app/public ./public
  RUN chmod 777 /app/public  # Ensure the directory is writable
  
  RUN mkdir .next
  RUN 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
  
  # Copy the startup script into the container and give it execution permission
  COPY ./frontend/web/copy_apk.sh /app/copy_apk.sh
  RUN chmod +x /app/copy_apk.sh
  
  # Use the startup script as the entrypoint (run it as root)
  USER root
  ENTRYPOINT ["/app/copy_apk.sh"]
  
  # Switch to `nextjs` user after running the script
  USER nextjs
  
  ENV WEB_PORT=${WEB_PORT}
  
  EXPOSE ${WEB_PORT}
  
  CMD ["node", "server.js"]
  