Getting EACCES: permission denied, mkdir '/app/.next/cache/images'

I recently deployed my Next.js app via a dockerfile.
Below you can see the dockerfile.


FROM node:16-alpine AS builder
# Check https://github.com/nodejs/docker-node/tree/b4117f9333da4138b03a546ec926ef50a31506c3#nodealpine to understand why libc6-compat might be needed.
RUN apk add --no-cache libc6-compat
WORKDIR /app
COPY . .

# If using npm with a `package-lock.json` comment out above and use below instead
RUN npm ci

ENV NEXT_TELEMETRY_DISABLED 1


# If using npm comment out above and use below instead

RUN npx prisma migrate deploy

RUN npx prisma generate
RUN npm run build

# Production image, copy all the files and run next
FROM node:16-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 /app ./

USER nextjs

ENV PORT 3000

CMD ["npm", "run","start"]

Everything is running fine but I am getting these errors in my logs:

2022-04-26T12:46:56.408 app[2bbc2a76] maa [info] Failed to write image to cache WebTu42A6KiKAt3CuJRb11ifqOS7zrDpScVm6miEBwM= [Error: EACCES: permission denied, mkdir '/app/.next/cache/images'] {

2022-04-26T12:46:56.408 app[2bbc2a76] maa [info]   errno: -13,

2022-04-26T12:46:56.408 app[2bbc2a76] maa [info]   code: 'EACCES',

2022-04-26T12:46:56.408 app[2bbc2a76] maa [info]   syscall: 'mkdir',

2022-04-26T12:46:56.408 app[2bbc2a76] maa [info]   path: '/app/.next/cache/images'

2022-04-26T12:46:56.408 app[2bbc2a76] maa [info] }

I have a features in my which uses the local file storage(public folder) for temp uploads of txt files.( The feature is not implemented yet so I cant test it).

Any help to remove this permission denied error is appreciated.

Your Dockerfile looks fine at first glance. I haven’t tried to deploy using Prisma yet but it doesn’t seem to be the issue here.

That being said. How’s your .dockerignore? Maybe there’s a chance you’re deploying your local .next folder to the Docker build.

Here’s my suggested .dockerignore:

Dockerfile
.dockerignore
node_modules
npm-debug.log
README.md
.next
.git

No, my .dockerignore includes .next folder. Even tried re-deploying but same issue. Entire log is filled with these messages.

One thing we can try is to create the folder beforehand

Add this to your Dockerfile:

RUN mkdir -p /app/.next/cache/images

I’d suggest doing this after RUN npm run build

For reference: [docker] permission denied, mkdir '/xx/xx/.next/cache' · Discussion #19188 · vercel/next.js · GitHub

Fixed it.

COPY --chown=nextjs:nodejs --from=builder /app/ ./

Turns it out with some permission issue indeed.

4 Likes