WeightTracker/Dockerfile

67 lines
2.1 KiB
Docker

# 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 Supervisor configuration
COPY supervisord.conf /etc/supervisor/conf.d/supervisord.conf
# Copy static website content (HTML, CSS, JS for the frontend)
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 app directory for Node.js API and copy from builder stage
WORKDIR /app # Make sure this WORKDIR is set before copying to /app
COPY --from=builder /app .
RUN ls -la /app # DEBUG: List contents of /app before supervisor starts
# Create data directory (volume will be mounted here by docker-compose)
# Ensure Nginx (and Node.js if it needs to write logs/pid here) has permissions
RUN mkdir -p /data && chown nginx:nginx /data # Or appropriate user for Node.js if it writes here
VOLUME /data
# Expose port 80 for Nginx (which handles auth and proxies to API)
EXPOSE 80
# Start Supervisor when the container launches
CMD ["/usr/bin/supervisord", "-c", "/etc/supervisor/conf.d/supervisord.conf"]