Files
2026-06-24 14:53:14 +02:00

81 lines
2.4 KiB
Docker

# Frontend Dockerfile for IoT PreTester V3
# Next.js-based React application with TypeScript
# Stage 1: Dependencies
FROM node:18-alpine AS deps
WORKDIR /app
# Copy package files
COPY Frontend/iot-pre-tester-frontend/package*.json ./
# Install dependencies (including devDependencies — typescript, tailwindcss
# and postcss are required for `next build`; the runner stage only ships the
# standalone output, so the final image stays slim)
RUN npm ci
# Stage 2: Builder
FROM node:18-alpine AS builder
WORKDIR /app
# Copy dependencies from deps stage
COPY --from=deps /app/node_modules ./node_modules
# Copy source code
COPY Frontend/iot-pre-tester-frontend/ ./
# Set build-time environment variable
ARG NEXT_PUBLIC_API_URL
ENV NEXT_PUBLIC_API_URL=${NEXT_PUBLIC_API_URL:-http://localhost:8000/api}
# Build Next.js application
RUN npm run build
# Stage 3: Runner
FROM node:18-alpine AS runner
WORKDIR /app
# Set production environment
# HOSTNAME=0.0.0.0 is required: the standalone server binds to $HOSTNAME,
# which Docker otherwise sets to the container id, so the server would only
# listen on the container IP and the loopback healthcheck would fail
ENV NODE_ENV=production \
PORT=3000 \
HOSTNAME=0.0.0.0
# Use existing node user (UID 1000, GID 1000) - matches backend
# No need to create new user, alpine node image already has it
# Copy standalone build from builder
COPY --from=builder /app/.next/standalone ./
COPY --from=builder /app/.next/static ./.next/static
COPY --from=builder /app/public ./public
# Create all required directories with proper permissions BEFORE switching user
RUN mkdir -p /app/public/filesAttachment /app/public/wireshark_capture /files && \
chown -R node:node /app /files && \
chmod 777 /app/public/filesAttachment && \
chmod 777 /app/public/wireshark_capture && \
chmod 777 /files
# Copy and set up entrypoint script
COPY docker/entrypoint-frontend.sh /app/entrypoint.sh
RUN chmod +x /app/entrypoint.sh && \
chown node:node /app/entrypoint.sh
# Switch to non-root user AFTER setting all permissions
USER node
# Expose port
EXPOSE 3000
# Health check
HEALTHCHECK --interval=30s --timeout=10s --start-period=20s --retries=3 \
CMD wget --no-verbose --tries=1 --spider http://127.0.0.1:3000/devices || exit 1
# Use entrypoint script to ensure permissions and start Next.js
ENTRYPOINT ["/app/entrypoint.sh"]
CMD ["node", "server.js"]