65 lines
1.5 KiB
Docker
65 lines
1.5 KiB
Docker
# Dockerfile for Astro webapp with NGINX and API for content management
|
|
# Stage 1: Build the Astro application
|
|
FROM node:20-alpine as build
|
|
|
|
# Set working directory
|
|
WORKDIR /app
|
|
|
|
# Copy package files and install dependencies
|
|
COPY package.json package-lock.json ./
|
|
RUN npm ci
|
|
|
|
# Copy the rest of the application code
|
|
COPY . .
|
|
|
|
# Build the Astro project
|
|
RUN npm run build
|
|
|
|
# Stage 2: Serve with NGINX and Node API
|
|
FROM nginx:alpine
|
|
|
|
# Install Node.js for the API server
|
|
RUN apk add --update nodejs npm
|
|
|
|
# Set up directory structure
|
|
WORKDIR /app
|
|
|
|
# Copy built static files from build stage to NGINX html directory
|
|
COPY --from=build /app/dist /usr/share/nginx/html
|
|
|
|
# Copy API files
|
|
COPY --from=build /app/src/api /app/api
|
|
WORKDIR /app/api
|
|
RUN npm install --production
|
|
|
|
# Add custom NGINX configuration with API proxy
|
|
RUN echo 'server {\
|
|
listen 80;\
|
|
server_name _;\
|
|
root /usr/share/nginx/html;\
|
|
index index.html;\
|
|
location / {\
|
|
try_files $uri $uri/ /index.html;\
|
|
}\
|
|
location /api/ {\
|
|
proxy_pass http://localhost:3000/api/;\
|
|
proxy_http_version 1.1;\
|
|
proxy_set_header Upgrade $http_upgrade;\
|
|
proxy_set_header Connection "upgrade";\
|
|
proxy_set_header Host $host;\
|
|
}\
|
|
}' > /etc/nginx/conf.d/default.conf
|
|
|
|
# Copy start script
|
|
COPY --from=build /app/start.sh /app/start.sh
|
|
RUN chmod +x /app/start.sh
|
|
|
|
# Create a directory for content if it doesn't exist
|
|
RUN mkdir -p /app/content/books
|
|
|
|
# Expose port 80
|
|
EXPOSE 80
|
|
|
|
# Start NGINX and API server
|
|
CMD ["/app/start.sh"]
|