Skip to content

Fix: grant nextjs user write access to .next/cache#5

Merged
crarau merged 1 commit into
mainfrom
fix/nextjs-cache-permissions
Apr 21, 2026
Merged

Fix: grant nextjs user write access to .next/cache#5
crarau merged 1 commit into
mainfrom
fix/nextjs-cache-permissions

Conversation

@crarau

@crarau crarau commented Apr 21, 2026

Copy link
Copy Markdown
Contributor

Root Cause

The Next.js Image Optimization cache writes to /app/.next/cache at runtime. In the multi-stage Dockerfile, the .next/ directory is created by COPY --from=builder and is owned by root. After switching to USER nextjs, the process cannot create the cache subdirectory, resulting in EACCES: permission denied errors.

Fix

Added RUN mkdir -p .next/cache && chown nextjs:nodejs .next/cache before the USER nextjs directive. This pre-creates the cache directory with correct ownership so the non-root user can write to it at runtime.

Alert Reference

Triggered by production alert at 2026-04-21T01:30:03.953Z — 3 EACCES errors in 5 minutes on c3-marketing.

Testing

  • All 158 unit/integration tests pass (npm test)
  • This is a standard Next.js Docker pattern fix (ref: Next.js docs on Docker deployment)
  • No unit test for filesystem permissions is feasible without a Docker build; the fix is a one-line well-established pattern

The image optimization cache writes to /app/.next/cache at runtime.
The directory inherits root ownership from the COPY layer, so the
non-root nextjs user gets EACCES. Create the directory and assign
ownership before switching USER.
@crarau
crarau merged commit 9ee703e into main Apr 21, 2026
2 checks passed
@crarau
crarau deleted the fix/nextjs-cache-permissions branch April 21, 2026 01:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant