You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

NextJs Docker化部署遇权限拒绝错误(sh: next: Permission denied)

问题:Docker构建Next.js 13应用时出现"next: Permission denied"错误

执行docker-compose up -d时遇到如下错误:

=> ERROR [builder 4/4] RUN npm run build                                                                                                             13.4s
------
 > [builder 4/4] RUN npm run build:
#0 11.75 
#0 11.75 > final-year-project@0.1.0 build
#0 11.75 > next build
#0 11.75 
#0 11.81 sh: next: Permission denied
------
failed to solve: process "/bin/sh -c npm run build" did not complete successfully: exit code: 126

使用的Dockerfile

FROM node:18-alpine AS base

# Install dependencies only when needed
FROM base AS deps
# 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

# Install dependencies based on the preferred package manager
COPY package.json yarn.lock* package-lock.json* pnpm-lock.yaml* ./
RUN \
  if [ -f yarn.lock ]; then yarn --frozen-lockfile; \
  elif [ -f package-lock.json ]; then npm ci; \
  elif [ -f pnpm-lock.yaml ]; then yarn global add pnpm && pnpm i --frozen-lockfile; \
  else echo "Lockfile not found." && exit 1; \
  fi


# Rebuild the source code only when needed
FROM base AS builder
WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules
COPY . .

# Next.js collects completely anonymous telemetry data about general usage.
# Learn more here: https://nextjs.org/telemetry
# Uncomment the following line in case you want to disable telemetry during the build.
# ENV NEXT_TELEMETRY_DISABLED 1

#RUN yarn build

# If using npm comment out above and use below instead
RUN npm run build

# Production image, copy all the files and run next
FROM base AS runner
WORKDIR /app

ENV NODE_ENV production
# Uncomment the following line in case you want to disable telemetry during runtime.
# ENV NEXT_TELEMETRY_DISABLED 1

RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 nextjs

COPY --from=builder /app/public ./public

# Set the correct permission for prerender cache
RUN mkdir .next
RUN chown nextjs:nodejs .next

# Automatically leverage output traces to reduce image size
# https://nextjs.org/docs/advanced-features/output-file-tracing
COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static

USER nextjs

EXPOSE 3000

ENV PORT 3000
# set hostname to localhost
ENV HOSTNAME "0.0.0.0"

CMD ["node", "server.js"]

使用的docker-compose.yml

version: '3'
services: 
  frontend:
    build: 
      context: ./frontend
    ports:
      - "3000:3000"
      
    depends_on:
      - orchastrator
      - db
      - datalake
      - mock_ml
    
  db:
    image: mongo
    ports:
      - "27017:27017"
    container_name: fyp-db  
    environment:
      MONGO_INITDB_ROOT_USERNAME: admin
      MONGO_INITDB_ROOT_PASSWORD: admin
    
  orchastrator:
    build: 
      context: ./orchastrator
    ports:
      - "8080:8080"
    depends_on:
      - db
  
  datalake:
    image: minio/minio
    ports:
      - "9000:9000"
      - "9001:9001"
    volumes:
      - minio_storage:/data
    environment:
      MINIO_ROOT_USER: luke
      MINIO_ROOT_PASSWORD: JHBUfabifiuabhibakjsfbiuy@@@@
    command: server --console-address ":9001" /data

  mock_ml:
    build: 
      context: ./mock-ml
    
    depends_on:
      - datalake
      - orchastrator
      - db

volumes:
  minio_storage: {}

解决方案

针对next: Permission denied的权限问题,可尝试以下几种解决方法:

  • 方法1:直接用npx调用next
    修改Dockerfile中builder阶段的RUN npm run build为:

    RUN npx next build
    

    绕过npm脚本的PATH查找,直接调用本地node_modules中的next命令。

  • 方法2:修复next命令的执行权限
    在builder阶段复制完node_modules后,添加命令赋予next可执行权限:

    COPY --from=deps /app/node_modules ./node_modules
    RUN chmod +x ./node_modules/.bin/next
    COPY . .
    RUN npm run build
    
  • 方法3:调整文件复制时的权限
    本地文件权限可能在复制到容器后丢失,在COPY项目文件时指定权限:

    COPY --chown=root:root . .
    

    确保builder阶段(默认root用户)拥有文件的完整权限。

  • 方法4:确认package.json配置
    检查package.json中的build脚本是否为"build": "next build",且next已正确安装在devDependencies中,执行npm ci时没有遗漏依赖。

内容的提问来源于stack exchange,提问作者Luke Connolly

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.08 05:33:22