使用来自Next的官方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 npm run 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
# 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
# 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
CMD ["node", "server.js"]
我在尝试使用Dockerfile和docker-compose构建应用程序时遇到以下错误
b78-client-1 | node:internal/modules/cjs/loader:1078
b78-client-1 | throw err;
b78-client-1 | ^
b78-client-1 |
b78-client-1 | Error: Cannot find module '/app/server.js'
b78-client-1 | at Module._resolveFilename (node:internal/modules/cjs/loader:1075:15)
b78-client-1 | at Module._load (node:internal/modules/cjs/loader:920:27)
b78-client-1 | at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
b78-client-1 | at node:internal/main/run_main_module:23:47 {
b78-client-1 | code: 'MODULE_NOT_FOUND',
b78-client-1 | requireStack: []
b78-client-1 | }
b78-client-1 |
b78-client-1 | Node.js v18.16.0
b78-client-1 exited with code 1
这是我的next.config.js
const withBundleAnalyzer = require('@next/bundle-analyzer')({
enabled: process.env.ANALYZE === 'true',
});
module.exports = withBundleAnalyzer({
reactStrictMode: false,
eslint: {
ignoreDuringBuilds: true,
},
output: "standalone",
});
我没有使用beta应用程序目录如果我尝试在CMD [“node”,“server.js”]之外运行镜像,使用镜像创建一个容器并输入SH,然后使用node运行server.js,它启动正常
~/Desktop/b78 on master !4 ❯ docker run --rm -it --entrypoint /bin/sh nextjsimg
/app $ ls
node_modules package.json public server.js
/app $ ls public
assets favicon.svg
/app $ ls -a
. .. .next node_modules package.json public server.js
/app $ ls .next
BUILD_ID package.json react-loadable-manifest.json routes-manifest.json static
build-manifest.json prerender-manifest.json required-server-files.json server
/app $ node server.js
1条答案
按热度按时间szqfcxe21#
看起来
CMD ["node", "server.js"]
应用到错误的路径。在运行服务器之前,您应该更改目录。