# Build stage
FROM node:18-alpine AS builder
WORKDIR /app
COPY package*.json ./
RUN npm install
RUN npm install sharp
COPY . .
RUN npm run build

# Production stage
FROM node:18-alpine AS runner
WORKDIR /app

ENV NODE_ENV production

RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 nextjs

# Copy necessary files
COPY --from=builder /app/next.config.js ./
COPY --from=builder /app/public ./public
COPY --from=builder /app/.next/standalone ./
COPY --from=builder /app/.next/static ./.next/static
COPY --from=builder /app/data ./data

# Create .next/cache directory and set permissions
RUN mkdir -p ./.next/cache
RUN chown -R nextjs:nodejs ./data
RUN chown -R nextjs:nodejs ./.next/cache
RUN chmod -R 755 ./data
RUN chmod -R 755 ./.next/cache

USER nextjs

EXPOSE 3000
ENV PORT 3000
ENV HOSTNAME "0.0.0.0"

CMD ["node", "server.js"]

# Hinweis:
# Bei Next.js übernimmt Node.js den Webserver für die Auslieferung der Seiten.
# Du brauchst KEINEN zusätzlichen Webserver wie nginx oder apache im Container.
# Traefik leitet die Anfragen an den Node.js-Server im Container weiter (Port 3000).
