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.
This commit is contained in:
SJCoetz 2026-07-23 19:26:49 +02:00
parent bb97fef453
commit e9b5324e1c

View file

@ -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