# Dockerfile for Weight Tracker App with Nginx, Basic Auth, and Node.js API # Stage 1: Build Node.js app and generate package-lock.json FROM node:alpine AS builder WORKDIR /app-builder # Copy package.json (and package-lock.json if it exists, though npm install will regenerate/respect it) COPY package.json ./ # If you have an .npmrc file for private registries or specific npm configurations, copy it here: # COPY .npmrc ./ RUN npm install # Now /app-builder contains package.json, package-lock.json, and node_modules # Stage 2: Final image with Nginx and the Node.js API FROM nginx:alpine # Install Supervisor, Node.js runtime (for running the API), and apache2-utils (for htpasswd) # npm is also needed here for 'npm install --production' RUN apk add --no-cache supervisor nodejs npm apache2-utils # Remove default Nginx configuration RUN rm /etc/nginx/conf.d/default.conf # Copy Nginx configuration files COPY nginx.conf /etc/nginx/nginx.conf COPY nginx-auth.conf /etc/nginx/nginx-auth.conf # Setup Basic Authentication # For production, consider environment variables or Docker secrets for credentials ARG AUTH_USERNAME ARG AUTH_PASSWORD RUN htpasswd -cb /etc/nginx/.htpasswd ${AUTH_USERNAME} ${AUTH_PASSWORD} \ && chown nginx:nginx /etc/nginx/.htpasswd \ && chmod 600 /etc/nginx/.htpasswd # Copy Supervisor configuration COPY supervisord.conf /etc/supervisor/conf.d/supervisord.conf # Set up Node.js application for the API in /app WORKDIR /app # Copy package.json and package-lock.json from the builder stage COPY --from=builder /app-builder/package.json ./ COPY --from=builder /app-builder/package-lock.json ./ # Install only production dependencies using the copied package-lock.json RUN npm install --production # Copy the server API script from the build context (your local machine) COPY server-api.js ./ # Copy static website content (HTML, CSS, JS for the frontend) # These are copied from your local build context COPY index.html /usr/share/nginx/html/ COPY js/ /usr/share/nginx/html/js/ COPY css/ /usr/share/nginx/html/css/ # Add other static asset directories if they exist (e.g., images, fonts, libs) # COPY images/ /usr/share/nginx/html/images/ # Create data directory (volume will be mounted here by docker-compose) RUN mkdir -p /data && chown nginx:nginx /data VOLUME /data # Copy and set up entrypoint script COPY entrypoint.sh /usr/local/bin/entrypoint.sh RUN chmod +x /usr/local/bin/entrypoint.sh ENTRYPOINT ["/usr/local/bin/entrypoint.sh"] # Expose port 80 for Nginx EXPOSE 80 # Start Supervisor (this will be passed to entrypoint.sh) CMD ["/usr/bin/supervisord", "-c", "/etc/supervisor/conf.d/supervisord.conf"]