refactor: streamline Dockerfile by consolidating dependencies and simplifying API setup

This commit is contained in:
greg 2025-05-24 08:55:28 +02:00
parent 90f4133606
commit a4bada5bc4

View File

@ -1,14 +1,13 @@
# Dockerfile for Astro webapp with NGINX and API for content management
# Stage 1: Build the Astro application
FROM node:20-alpine as build
FROM node:20-alpine AS build # Fixed 'as' to 'AS'
WORKDIR /app
# Copy package files. If src/api has its own package.json, handle it here or in a dedicated API build stage.
# Copy root package files. Assumes API dependencies are in the root package.json.
COPY package.json package-lock.json* ./
# Ensure src/api/package.json is copied if it exists and is used for API dependencies
# COPY src/api/package.json src/api/package-lock.json* ./src/api/
# Install ALL dependencies (including for API) from root package-lock.json
RUN npm ci
# Copy the rest of the application code
@ -18,42 +17,33 @@ COPY . .
# Build the Astro project (generates into /app/dist)
RUN npm run build
# If API has separate build step, include it here.
# Example: RUN npm run build --workspace=api (if using npm workspaces)
# Stage 2: Serve with NGINX and Node API
FROM nginx:alpine
# Create a non-root user and group
RUN addgroup -S appgroup && adduser -S -G appgroup appuser
# Install Node.js for the API server and su-exec for user switching
RUN apk add --no-cache nodejs npm su-exec
# Install Node.js for the API server (su-exec is not strictly needed if USER appuser is used for CMD)
RUN apk add --no-cache nodejs npm # su-exec can be removed if not used elsewhere
# Set base working directory
# Set base working directory for the final stage
WORKDIR /app
# Copy built static files from build stage to NGINX html directory
COPY --from=build /app/dist /usr/share/nginx/html
RUN chown -R appuser:appgroup /usr/share/nginx/html && chmod -R 755 /usr/share/nginx/html
# Copy API files from build stage
# Ensure that node_modules for the API are correctly copied or installed here.
# Copy API source files from build stage
COPY --from=build /app/src/api /app/api
# If API node_modules were part of the build stage under /app/api/node_modules, copy them:
# COPY --from=build --chown=appuser:appgroup /app/api/node_modules /app/api/node_modules
# Or, if API has its own package.json and it was copied to /app/api in build stage:
COPY --from=build /app/api/package.json /app/api/package-lock.json* /app/api/
WORKDIR /app/api
RUN npm ci --omit=dev --ignore-scripts # Install production API dependencies
RUN chown -R appuser:appgroup /app/api
# Copy node_modules from the build stage (contains all dependencies)
# The API running in /app/api will be able to resolve modules from /app/node_modules
COPY --from=build --chown=appuser:appgroup /app/node_modules /app/node_modules
# Copy custom NGINX configuration
# This replaces the RUN echo '...' command
COPY nginx.conf /etc/nginx/conf.d/default.conf
RUN chown appuser:appgroup /etc/nginx/conf.d/default.conf && chmod 644 /etc/nginx/conf.d/default.conf
# Ensure Nginx can write to its log directory (usually /var/log/nginx)
# Alpine Nginx base image usually sets this up correctly. If not, create and chown /var/log/nginx.
RUN mkdir -p /var/log/nginx && chown -R appuser:appgroup /var/log/nginx
RUN mkdir -p /run/nginx && chown -R appuser:appgroup /run/nginx # For PID file
@ -62,7 +52,9 @@ COPY --from=build /app/start.sh /app/start.sh
RUN chmod +x /app/start.sh && chown appuser:appgroup /app/start.sh
# Create and set permissions for content directory
RUN mkdir -p /app/content/books && chown -R appuser:appgroup /app/content
# This WORKDIR /app is important for relative paths in start.sh if any
WORKDIR /app
RUN mkdir -p ./content/books && chown -R appuser:appgroup ./content
# Expose port 80 (Nginx will listen on this port)
EXPOSE 80
@ -71,6 +63,5 @@ EXPOSE 80
USER appuser
# Start NGINX and API server using the start script
# The start.sh script will use su-exec for the Node API part if needed,
# but since we USER appuser, nginx and node will run as appuser.
# Both processes will run as 'appuser'
CMD ["/app/start.sh"]