I’m facing a permission denied error when running my Next.js application with Docker Compose. The error message I’m encountering is as follows:
[+] Running 2/2
✔ Network nextjs-blog_default Created 0.1s
✔ Container nextjs-blog-web-1 Created 0.1s
Attaching to web-1
web-1 |
web-1 | > dev
web-1 | > next dev
web-1 |
web-1 | ▲ Next.js 14.1.0
web-1 | - Local: http://localhost:3000
web-1 |
web-1 | [Error: EACCES: permission denied, unlink '/app/.next/build-manifest.json'] {
web-1 | errno: -13,
web-1 | code: 'EACCES',
web-1 | syscall: 'unlink',
web-1 | path: '/app/.next/build-manifest.json'
web-1 | }
web-1 |
web-1 exited with code 0
This seems to be related to file permissions within the .next
directory of my Next.js application. I believe it’s occurring because of how Docker handles file permissions with the account nextjs
in the file Dockerfile
.
I create the Nextjs project at the link: https://nextjs.org/learn-pages-router/basics/create-nextjs-app/setup. The latest version of Nextjs is 14.1.0
npx create-next-app@latest nextjs-blog --use-npm --example "https://github.com/vercel/next-learn/tree/main/basics/learn-starter"
If run by command npm run dev
is normally.
My environment:
Ubuntu: 22.04.3 LTS
Docker: 25.0.2
Docker Compose: v2.24.5
Here’s my Dockerfile
:
FROM node:18-alpine AS deps
RUN apk add --no-cache libc6-compat
WORKDIR /app
COPY package.json package-lock.json ./
RUN npm install --production
FROM node:18-alpine AS builder
WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules
COPY . .
ENV NEXT_TELEMETRY_DISABLED 1
RUN npm run build
FROM node:18-alpine AS runner
WORKDIR /app
ENV NODE_ENV production
ENV NEXT_TELEMETRY_DISABLED 1
RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 nextjs
COPY --from=builder --chown=nextjs:nodejs /app/.next ./.next
COPY --from=builder /app/node_modules ./node_modules
COPY --from=builder /app/package.json ./package.json
USER nextjs
EXPOSE 3000
ENV PORT 3000
CMD ["npm", "start"]
And here’s my docker-compose.yml
file:
version: '3.8'
services:
web:
build:
context: ./
target: runner
volumes:
- .:/app
command: npm run dev
ports:
- "3000:3000"
environment:
NODE_ENV: development
Build image by command: docker compose build
and run docker compose up
Could someone help me figure out how to properly handle file permissions in my Docker setup for Next.js applications? Thank you!
2
Answers
You can put the .next folder in a named volume to avoid this kind of issue
For example you can use this config for
docker-compose.yml
:The
volumes:
block hides everything in the image in the/app
directory, which is to say, nothing in the Dockerfile has an effect. Where youCOPY --chown=...
files into the final image in the Dockerfile, that’s hidden by the bind mount, and you get whatever permissions the directory has on the host system.Since all of the code is built into the image, you don’t need this
volumes:
block at all. You note in a comment that the image’sCMD
is also correct, and the target image is the final stage in the Dockerfile. You should be able to trim the Compose file down to just