fix: improve Docker build process by using ARGs correctly and adding Next.js config file

This commit is contained in:
Greg 2025-05-18 16:49:16 +02:00
parent 4ff3b63738
commit 7ad27799f9
2 changed files with 24 additions and 22 deletions

View File

@ -1,5 +1,6 @@
# Production-ready Dockerfile for Next.js # Production-ready Dockerfile for Next.js
# Receive build arguments passed by Coolify
# Receive build arguments passed by Coolify for the builder stage
ARG NEXT_PUBLIC_SUPABASE_URL ARG NEXT_PUBLIC_SUPABASE_URL
ARG NEXT_PUBLIC_SUPABASE_ANON_KEY ARG NEXT_PUBLIC_SUPABASE_ANON_KEY
@ -7,41 +8,47 @@ ARG NEXT_PUBLIC_SUPABASE_ANON_KEY
FROM node:20-alpine AS builder FROM node:20-alpine AS builder
WORKDIR /app WORKDIR /app
# Set environment variables for the builder stage from build arguments # These ARGs are available to RUN commands in this stage
ARG NEXT_PUBLIC_SUPABASE_URL
ARG NEXT_PUBLIC_SUPABASE_ANON_KEY
ENV NODE_ENV=production ENV NODE_ENV=production
ENV NEXT_PUBLIC_SUPABASE_URL=$NEXT_PUBLIC_SUPABASE_URL
ENV NEXT_PUBLIC_SUPABASE_ANON_KEY=$NEXT_PUBLIC_SUPABASE_ANON_KEY
# Copy package.json and package-lock.json from the 'myfavstuff' subdirectory # Copy package.json and package-lock.json from the 'myfavstuff' subdirectory
COPY myfavstuff/package.json myfavstuff/package-lock.json* ./ COPY myfavstuff/package.json myfavstuff/package-lock.json* ./
# Install dependencies including dev dependencies needed for build # Install dependencies including dev dependencies needed for build
# npm ci should install eslint from devDependencies
RUN npm ci RUN npm ci
# Copy the rest of your app's source code from the 'myfavstuff' subdirectory # Copy the rest of your app's source code from the 'myfavstuff' subdirectory
COPY myfavstuff . COPY myfavstuff .
# Debug the environment variables # Debug the environment variables that npm run build will see
RUN echo "--- Debugging Build Environment Variables (Builder Stage) ---" && \ # Pass ARGs directly to the build command's environment
echo "NEXT_PUBLIC_SUPABASE_URL: ($NEXT_PUBLIC_SUPABASE_URL)" && \ RUN echo "--- Debugging Build Environment Variables (Passed to Build) ---" && \
echo "NEXT_PUBLIC_SUPABASE_ANON_KEY: ($NEXT_PUBLIC_SUPABASE_ANON_KEY)" && \ echo "NEXT_PUBLIC_SUPABASE_URL (from ARG): $NEXT_PUBLIC_SUPABASE_URL" && \
echo "NEXT_PUBLIC_SUPABASE_ANON_KEY (from ARG): $NEXT_PUBLIC_SUPABASE_ANON_KEY" && \
echo "--- End Debugging --- " && \ echo "--- End Debugging --- " && \
NEXT_PUBLIC_SUPABASE_URL=$NEXT_PUBLIC_SUPABASE_URL \
NEXT_PUBLIC_SUPABASE_ANON_KEY=$NEXT_PUBLIC_SUPABASE_ANON_KEY \
npm run build npm run build
# Use a non-root user for security
RUN addgroup -g 1001 -S nodejs && adduser -S nextjs -u 1001
# Production stage # Production stage
FROM node:20-alpine AS runner FROM node:20-alpine AS runner
WORKDIR /app WORKDIR /app
ENV NODE_ENV=production ENV NODE_ENV=production
ENV NEXT_PUBLIC_SUPABASE_URL=$NEXT_PUBLIC_SUPABASE_URL # Runtime environment variables will be set by Coolify directly in the container environment.
ENV NEXT_PUBLIC_SUPABASE_ANON_KEY=$NEXT_PUBLIC_SUPABASE_ANON_KEY # We don't need to re-declare ARGs here or pass them from the builder stage for runtime ENV.
# Coolify injects them into the final container.
# Create a non-root user for security IN THIS STAGE
RUN addgroup -g 1001 -S nodejs && adduser -S nextjs -u 1001
COPY --from=builder /app/public ./public COPY --from=builder /app/public ./public
# Look for .next output (either standalone or regular)
COPY --from=builder --chown=nextjs:nodejs /app/.next ./.next COPY --from=builder --chown=nextjs:nodejs /app/.next ./.next
# If you decide to use standalone output later, you'd copy /app/.next/standalone and /app/.next/static
USER nextjs USER nextjs
@ -50,4 +57,5 @@ EXPOSE 3000
ENV PORT=3000 ENV PORT=3000
ENV HOSTNAME=0.0.0.0 ENV HOSTNAME=0.0.0.0
# The start script in package.json should be `next start`
CMD ["npm", "start"] CMD ["npm", "start"]

View File

@ -1,16 +1,10 @@
/** @type {import('next').NextConfig} */ /** @type {import('next').NextConfig} */
const nextConfig = { const nextConfig = {
// Enable output.standalone to create a standalone build // output: 'standalone', // Consider this if you want a smaller Docker image, requires Next.js 12.2+
// This is recommended for deployments to containerized environments
output: 'export',
// Set to true for improved page load performance
swcMinify: true,
// Configure images if using them from external domains // Configure images if using them from external domains
images: { images: {
unoptimized: true, // For static export // domains: ['example.com'], // Add your image domains here if needed
domains: [],
}, },
// Add any other configurations needed // Add any other configurations needed