refactor: streamline Dockerfile by consolidating dependencies and simplifying API setup
This commit is contained in:
parent
90f4133606
commit
a4bada5bc4
39
Dockerfile
39
Dockerfile
@ -1,14 +1,13 @@
|
|||||||
# Dockerfile for Astro webapp with NGINX and API for content management
|
# Dockerfile for Astro webapp with NGINX and API for content management
|
||||||
# Stage 1: Build the Astro application
|
# Stage 1: Build the Astro application
|
||||||
FROM node:20-alpine as build
|
FROM node:20-alpine AS build # Fixed 'as' to 'AS'
|
||||||
|
|
||||||
WORKDIR /app
|
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* ./
|
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
|
RUN npm ci
|
||||||
|
|
||||||
# Copy the rest of the application code
|
# Copy the rest of the application code
|
||||||
@ -18,42 +17,33 @@ COPY . .
|
|||||||
# Build the Astro project (generates into /app/dist)
|
# Build the Astro project (generates into /app/dist)
|
||||||
RUN npm run build
|
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
|
# Stage 2: Serve with NGINX and Node API
|
||||||
FROM nginx:alpine
|
FROM nginx:alpine
|
||||||
|
|
||||||
# Create a non-root user and group
|
# Create a non-root user and group
|
||||||
RUN addgroup -S appgroup && adduser -S -G appgroup appuser
|
RUN addgroup -S appgroup && adduser -S -G appgroup appuser
|
||||||
|
|
||||||
# Install Node.js for the API server and su-exec for user switching
|
# 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
|
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
|
WORKDIR /app
|
||||||
|
|
||||||
# Copy built static files from build stage to NGINX html directory
|
# Copy built static files from build stage to NGINX html directory
|
||||||
COPY --from=build /app/dist /usr/share/nginx/html
|
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
|
RUN chown -R appuser:appgroup /usr/share/nginx/html && chmod -R 755 /usr/share/nginx/html
|
||||||
|
|
||||||
# Copy API files from build stage
|
# Copy API source files from build stage
|
||||||
# Ensure that node_modules for the API are correctly copied or installed here.
|
|
||||||
COPY --from=build /app/src/api /app/api
|
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
|
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
|
# Copy custom NGINX configuration
|
||||||
# This replaces the RUN echo '...' command
|
|
||||||
COPY nginx.conf /etc/nginx/conf.d/default.conf
|
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
|
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 /var/log/nginx && chown -R appuser:appgroup /var/log/nginx
|
||||||
RUN mkdir -p /run/nginx && chown -R appuser:appgroup /run/nginx # For PID file
|
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
|
RUN chmod +x /app/start.sh && chown appuser:appgroup /app/start.sh
|
||||||
|
|
||||||
# Create and set permissions for content directory
|
# 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 port 80 (Nginx will listen on this port)
|
||||||
EXPOSE 80
|
EXPOSE 80
|
||||||
@ -71,6 +63,5 @@ EXPOSE 80
|
|||||||
USER appuser
|
USER appuser
|
||||||
|
|
||||||
# Start NGINX and API server using the start script
|
# Start NGINX and API server using the start script
|
||||||
# The start.sh script will use su-exec for the Node API part if needed,
|
# Both processes will run as 'appuser'
|
||||||
# but since we USER appuser, nginx and node will run as appuser.
|
|
||||||
CMD ["/app/start.sh"]
|
CMD ["/app/start.sh"]
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user