# Stage 1: Build Node.js application FROM node:18-alpine AS builder WORKDIR /app # Copy package.json and package-lock.json (if available) COPY package.json ./ # COPY package-lock.json ./ # Install dependencies RUN npm install --production # Copy the rest of the application files (API code) COPY data-api.js ./ # Add any other necessary files for the API here # Stage 2: Setup Nginx and Supervisor FROM nginx:alpine # Install Supervisor, Node.js runtime, and apache2-utils (for htpasswd) RUN apk add --no-cache supervisor nodejs npm apache2-utils # Remove default Nginx configuration RUN rm /etc/nginx/conf.d/default.conf # Declare build arguments for credentials ARG AUTH_USERNAME ARG AUTH_PASSWORD # Copy custom Nginx configuration COPY nginx.conf /etc/nginx/nginx.conf # Copy Basic Auth config (nginx-auth.conf) COPY nginx-auth.conf /etc/nginx/nginx-auth.conf # Generate .htpasswd file using build arguments RUN htpasswd -cb /etc/nginx/.htpasswd ${AUTH_USERNAME} ${AUTH_PASSWORD} \ && chown nginx:nginx /etc/nginx/.htpasswd \ && chmod 600 /etc/nginx/.htpasswd # Copy application files COPY --from=builder /app/data-api.js \ /usr/share/nginx/html/data-api.js \ /usr/share/nginx/html/auth-middleware.js \ /usr/share/nginx/html/backup-s3.js \ /usr/share/nginx/html/package.json \ /usr/share/nginx/html/package-lock.json \ /usr/share/nginx/html/node_modules \ /usr/share/nginx/html/.git \ /usr/share/nginx/html/.github \ /usr/share/nginx/html/.vscode \ /usr/share/nginx/html/README.md # Expose port 80 for HTTP traffic EXPOSE 80 # Command to start Nginx in the foreground # This ensures the container keeps running CMD ["nginx", "-g", "daemon off;"]