feat: optimize Docker build with Next.js standalone output configuration

This commit is contained in:
Greg 2025-05-18 17:32:59 +02:00
parent 16825648ed
commit 11188330fe
2 changed files with 11 additions and 10 deletions

View File

@ -18,7 +18,7 @@ ENV NODE_ENV=production
COPY myfavstuff/package.json myfavstuff/package-lock.json* ./
# Install dependencies including dev dependencies needed for build
# npm ci should install eslint from devDependencies
# This will also install eslint if it's in devDependencies and package-lock.json is up to date
RUN npm ci
# Copy the rest of your app's source code from the 'myfavstuff' subdirectory
@ -34,21 +34,22 @@ RUN echo "--- Debugging Build Environment Variables (Passed to Build) ---" && \
NEXT_PUBLIC_SUPABASE_ANON_KEY=$NEXT_PUBLIC_SUPABASE_ANON_KEY \
npm run build
# Production stage
# Production stage (runner)
FROM node:20-alpine AS runner
WORKDIR /app
ENV NODE_ENV=production
# Runtime environment variables will be set by Coolify directly in the container environment.
# 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 --chown=nextjs:nodejs /app/.next ./.next
# If you decide to use standalone output later, you'd copy /app/.next/standalone and /app/.next/static
# Copy standalone output from the builder stage
COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
# Copy static assets
COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static
# Copy public assets (if any, like favicon, images in /public)
COPY --from=builder --chown=nextjs:nodejs /app/public ./public
USER nextjs
@ -57,5 +58,5 @@ EXPOSE 3000
ENV PORT=3000
ENV HOSTNAME=0.0.0.0
# The start script in package.json should be `next start`
CMD ["npm", "start"]
# Run the Next.js standalone server
CMD ["node", "server.js"]

View File

@ -1,6 +1,6 @@
/** @type {import('next').NextConfig} */
const nextConfig = {
// output: 'standalone', // Consider this if you want a smaller Docker image, requires Next.js 12.2+
output: 'standalone', // Enable standalone output for optimized Docker builds
// Configure images if using them from external domains
images: {