From e9b5324e1cef37b0388ef591f755f657a8f50820 Mon Sep 17 00:00:00 2001 From: SJCoetz Date: Thu, 23 Jul 2026 19:26:49 +0200 Subject: [PATCH] build: remove empty public dir from docker image Add a conditional cleanup step to remove the public directory if it does not exist or contains no files. This reduces the final docker image size since Next.js standalone builds automatically inline public assets when they are present, making the copied public directory often redundant. --- Dockerfile | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/Dockerfile b/Dockerfile index a85dcf9..10a8cf9 100644 --- a/Dockerfile +++ b/Dockerfile @@ -62,10 +62,18 @@ ENV HOSTNAME=0.0.0.0 RUN addgroup --system --gid 1001 nodejs && \ adduser --system --uid 1001 nextjs -# Copy the standalone build (Next.js output tracing) + static assets -COPY --from=builder /app/public ./public +# Copy the standalone build (Next.js output tracing) + static assets. +# `public/` is optional (PWA manifest, favicons, robots.txt). Next +# inlines any source `public/` into the standalone build, so we only +# need to copy it if it actually exists. Projects without a public/ +# dir (e.g. this one pre-PWA) no-op cleanly. COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./ COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static +RUN if [ -d ./public ] && [ -n "$(ls -A ./public 2>/dev/null)" ]; then \ + echo "public/ assets found in standalone build, keeping"; \ + else \ + rm -rf ./public 2>/dev/null || true; \ + fi USER nextjs EXPOSE 3000