egentrening/Dockerfile

68 lines
1.8 KiB
Text
Raw Normal View History

2025-09-02 23:58:00 +02:00
# Use the official Node.js runtime as the base image
FROM node:20-alpine AS base
# Set the working directory inside the container
WORKDIR /app
# Copy package.json and package-lock.json (if available)
COPY package*.json ./
2025-09-03 00:00:02 +02:00
# Install all dependencies (including devDependencies for building)
RUN npm ci && npm cache clean --force
2025-09-02 23:58:00 +02:00
# Copy the rest of the application code
COPY . .
2025-09-03 00:41:40 +02:00
# Set build-time environment variables for SvelteKit
ARG DATABASE_URL="postgresql://builduser:buildpass@localhost:5432/builddb"
ENV DATABASE_URL=${DATABASE_URL}
2025-09-03 00:01:24 +02:00
# Run SvelteKit sync to generate .svelte-kit directory and prepare the build
RUN npm run prepare
2025-09-03 00:41:40 +02:00
# Build the application with environment variables available
2025-09-03 00:23:17 +02:00
RUN npm run build
2025-09-02 23:58:00 +02:00
# Production stage
FROM node:20-alpine AS production
# Install dumb-init for proper signal handling
RUN apk add --no-cache dumb-init
# Create a non-root user
RUN addgroup -g 1001 -S nodejs
RUN adduser -S svelte -u 1001
# Set the working directory
WORKDIR /app
# Copy package.json and package-lock.json
COPY package*.json ./
# Install only production dependencies
RUN npm ci --only=production && npm cache clean --force
# Copy the built application from the previous stage
COPY --from=base --chown=svelte:nodejs /app/build ./build
COPY --from=base --chown=svelte:nodejs /app/package.json ./package.json
2025-09-03 00:41:40 +02:00
# Copy environment file if it exists (optional)
COPY --chown=svelte:nodejs .env* ./ || true
2025-09-02 23:58:00 +02:00
# Switch to the non-root user
USER svelte
# Expose the port the app runs on
EXPOSE 3000
2025-09-03 00:41:40 +02:00
# Set environment variables - DATABASE_URL can be overridden at runtime
2025-09-02 23:58:00 +02:00
ENV NODE_ENV=production
ENV PORT=3000
2025-09-03 00:41:40 +02:00
ENV DATABASE_URL=""
2025-09-02 23:58:00 +02:00
# Use dumb-init to handle signals properly
ENTRYPOINT ["dumb-init", "--"]
2025-09-03 00:18:34 +02:00
# Start the application with environment file support
CMD ["node", "--env-file=.env", "build"]