63 lines
1.8 KiB
Docker
63 lines
1.8 KiB
Docker
# Base on official Node.js Alpine image
|
|
FROM node:18-alpine AS builder
|
|
|
|
# Set working directory
|
|
WORKDIR /app
|
|
|
|
# Copy package.json and package-lock.json before other files
|
|
# Utilize Docker cache to save re-installing dependencies if unchanged
|
|
COPY package*.json ./
|
|
|
|
# Install dependencies
|
|
RUN npm ci
|
|
|
|
# Copy all files
|
|
COPY . .
|
|
|
|
# Define build arguments with defaults
|
|
ARG NEXT_PUBLIC_SUPABASE_URL
|
|
ARG NEXT_PUBLIC_SUPABASE_ANON_KEY
|
|
|
|
# Set environment variables from build arguments
|
|
ENV NEXT_PUBLIC_SUPABASE_URL=$NEXT_PUBLIC_SUPABASE_URL
|
|
ENV NEXT_PUBLIC_SUPABASE_ANON_KEY=$NEXT_PUBLIC_SUPABASE_ANON_KEY
|
|
|
|
|
|
# Build app
|
|
RUN npm run build
|
|
|
|
# Production image, copy all the files and run next
|
|
FROM node:18-alpine AS runner
|
|
|
|
WORKDIR /app
|
|
|
|
# Set to production environment
|
|
ENV NODE_ENV=production
|
|
|
|
# Add user so we don't run as root
|
|
RUN addgroup --system --gid 1001 nodejs \
|
|
&& adduser --system --uid 1001 nextjs
|
|
|
|
# Copy built app
|
|
COPY --from=builder --chown=nextjs:nodejs /app/.next ./.next
|
|
COPY --from=builder --chown=nextjs:nodejs /app/node_modules ./node_modules
|
|
COPY --from=builder --chown=nextjs:nodejs /app/package.json ./package.json
|
|
COPY --from=builder --chown=nextjs:nodejs /app/public ./public
|
|
|
|
# Switch to non-root user
|
|
USER nextjs
|
|
|
|
# Expose port
|
|
EXPOSE 3000
|
|
|
|
# Start the app
|
|
CMD ["npm", "start"]
|
|
|
|
# Note: When running the container, override environment variables with:
|
|
# docker run -p 3000:3000 -e NEXT_PUBLIC_SUPABASE_URL=your_url -e NEXT_PUBLIC_SUPABASE_ANON_KEY=your_key nextjs-slack-clone
|
|
# Note: These lines correctly declare and set up build arguments.
|
|
# To load values for these arguments from a file, you would use
|
|
# external mechanisms (like a shell script parsing an .env file and passing
|
|
# --build-arg flags, or Docker Compose with its .env file handling)
|
|
# when invoking `docker build`. The Dockerfile itself does not need changes
|
|
# for this purpose.
|