Payload CMS容器部署Azure遇Docker Build错误:未知文件模式
Payload CMS容器化部署到Azure App Service时Docker Build报错问题
问题概述
尝试将Payload CMS应用容器化部署到Azure App Service,执行docker build时失败,报错ERROR: failed to solve: archive/tar: unknown file mode ?rwxr-xr-x
已做的必要修改
- next.config.js:添加
output: 'standalone'配置
const nextConfig = { images: { remotePatterns: [ ...[NEXT_PUBLIC_SERVER_URL /* 'https://example.com' */].map((item) => { const url = new URL(item) return { hostname: url.hostname, protocol: url.protocol.replace(':', ''), } }), ], }, reactStrictMode: true, redirects, output: 'standalone', }
- payload.config.ts:适配Azure Cosmos DB
- 在
mongooseAdapter中添加transactionOptions: false - 在
buildConfig中添加indexSortableFields: true
- 在
db: mongooseAdapter({ url: process.env.DATABASE_URI || '', transactionOptions: false, }),
使用的配置文件
Dockerfile
# To use this Dockerfile, you have to set `output: 'standalone'` in your next.config.js file. # From https://github.com/vercel/next.js/blob/canary/examples/with-docker/Dockerfile FROM node:22.12.0-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 corepack enable 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 \ if [ -f yarn.lock ]; then yarn run build; \ elif [ -f package-lock.json ]; then npm run build; \ elif [ -f pnpm-lock.yaml ]; then corepack enable pnpm && pnpm run build; \ else echo "Lockfile not found." && exit 1; \ fi # 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 # Remove this line if you do not have this folder 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 # server.js is created by next build from the standalone output # https://nextjs.org/docs/pages/api-reference/next-config-js/output # CMD HOSTNAME="0.0.0.0" node server.js ENV HOSTNAME="0.0.0.0" CMD ["node", "server.js"]
docker-compose.yml
version: '3' services: payload: image: node:18-alpine ports: - '3000:3000' volumes: - .:/home/node/app - node_modules:/home/node/app/node_modules working_dir: /home/node/app/ command: sh -c "yarn install && yarn dev" depends_on: - mongo env_file: - .env mongo: image: mongo:latest ports: - '27017:27017' command: - --storageEngine=wiredTiger volumes: - data:/data/db logging: driver: none volumes: data: node_modules:
执行的构建命令
az loginaz acr login --name <acr-name>docker build -t <acr>/<build-name>:<version-tag> .
完整错误信息
[+] Building 491.8s (10/20) docker:desktop-linux => [internal] load build definition from Dockerfile 0.0s => => transferring dockerfile: 2.53kB 0.0s => [internal] load metadata for docker.io/library/node:22.12.0-alpine 0.0s => [internal] load .dockerignore 0.0s => => transferring context: 2B 0.0s => ERROR [internal] load build context 488.9s => => transferring context: 1.45GB 488.9s => CACHED [base 1/1] FROM docker.io/library/node:22.12.0-alpine@sha256:51eff88af6dff26f59316b6e356188ffa2c422bd3c3b76f2556a2e7e89d080bd => => transferring dockerfile: 2.53kB 0.0s fa2c422bd3c3b76f2556a2e7e89d080bd => [internal] load metadata for docker.io/library/node:22.12.0-alpine 0.0s => [internal] load .dockerignore 0.0s 2.7s => => resolve docker.io/library/node:22.12.0-alpine@sha256:51eff88af6dff26f59316b6e356188ffa2c422bd3c3b76f2556a2e7e89d080bd 2.7s => CACHED [builder 1/4] WORKDIR /app 0.0s => [runner 2/8] RUN addgroup --system --gid 1001 nodejs 0.8s => [deps 1/4] RUN apk add --no-cache libc6-compat 1.8s => [runner 3/8] RUN adduser --system --uid 1001 nextjs 0.7s => [deps 2/4] WORKDIR /app 0.0s ------ > [internal] load build context: ------ ERROR: failed to solve: archive/tar: unknown file mode ?rwxr-xr-x
Docker版本:Docker version 27.5.1, build 9f9e405
解决方案
这个错误是Docker读取构建上下文时遇到权限异常的文件导致的,按以下步骤修复:
优化
.dockerignore文件
确保文件包含无需打包的目录,减少构建上下文体积并避免异常文件:node_modules .next .env .git dist *.log清理并重建依赖
删除可能存在异常的目录后重新安装依赖:rm -rf node_modules .next yarn install # 或 npm install / pnpm install排查特殊权限文件
查找项目中的符号链接或权限异常文件:find . -type l # 列出所有符号链接 ls -la # 查看文件权限详情对异常文件调整权限:
chmod 755 <异常文件路径>强制无缓存构建
如果缓存导致问题,执行无缓存构建:docker build --no-cache -t <acr>/<build-name>:<version-tag> .Windows环境适配
若在Windows上构建,确保使用WSL2,且项目目录位于WSL文件系统中,避免权限映射冲突。
内容的提问来源于stack exchange,提问作者Jason Susanto
相关产品推荐
相关产品推荐

