Docker image build successfully but after docker run : Couldn't find the node_modules state file – running an install might help (findPackageLocation)

huangapple go评论81阅读模式
英文:

Docker image build successfully but after docker run : Couldn't find the node_modules state file - running an install might help (findPackageLocation)

问题

我有一个使用默认的 yarn 3.5.1 的 Next JS 项目(它是一个模板)。我可以成功地通过 Docker 构建该项目,没有任何错误。以下是该项目的 Docker 文件:

# 阶段 1:构建
FROM node:18-alpine AS builder

WORKDIR /app

COPY .yarn ./.yarn
COPY .yarnrc.yml ./
COPY package.json yarn.lock ./

RUN yarn install --immutable

COPY . .

RUN yarn build

# 阶段 2:运行
FROM node:18-alpine

WORKDIR /app

COPY --from=builder /app/next.config.js ./
COPY --from=builder /app/public ./public
COPY --from=builder /app/.next ./.next
COPY --from=builder /app/.yarn ./.yarn
COPY --from=builder /app/.yarnrc.yml ./
COPY --from=builder /app/package.json ./package.json
COPY --from=builder /app/yarn.lock ./yarn.lock

ENV NODE_ENV=production

EXPOSE 3000

CMD ["yarn", "start"]

但是,当我尝试运行这个镜像容器时,我收到以下错误消息:

$ sudo docker run --restart=always -p 8080:80 next1
Usage Error: 找不到 node_modules 状态文件 - 运行安装可能会有所帮助 (findPackageLocation)

$ yarn run [--inspect] [--inspect-brk] [-T,--top-level] [-B,--binaries-only] <scriptName> ...

我在 Stack Overflow 上找到了一个类似的问题,基于那个问题,我尝试使用不同的 Node 版本,但问题没有解决。如果有任何提示或想法,将不胜感激。

英文:

I have a Next JS project which uses yarn 3.5.1 by default (it was a template) I can build the project via docker build successfully without any error. here is my docker file for this project:

# Stage 1: Build
FROM node:18-alpine AS builder

WORKDIR /app

COPY .yarn ./.yarn
COPY .yarnrc.yml ./
COPY package.json yarn.lock ./

RUN yarn install --immutable

COPY . .

RUN yarn build

# Stage 2: Run
FROM node:18-alpine

WORKDIR /app

COPY --from=builder /app/next.config.js ./
COPY --from=builder /app/public ./public
COPY --from=builder /app/.next ./.next
COPY --from=builder /app/.yarn ./.yarn
COPY --from=builder /app/.yarnrc.yml ./
COPY --from=builder /app/package.json ./package.json
COPY --from=builder /app/yarn.lock ./yarn.lock

ENV NODE_ENV=production

EXPOSE 3000

CMD [&quot;yarn&quot;, &quot;start&quot;]

but when I want to run this image container I get this error message:

$ sudo docker run --restart=always -p 8080:80 next1
Usage Error: Couldn&#39;t find the node_modules state file - running an install might help (findPackageLocation)

$ yarn run [--inspect] [--inspect-brk] [-T,--top-level] [-B,--binaries-only] &lt;scriptName&gt; ...

I found a similar question in SO based on that I tried to use a different node version but issue not resolved. any tips or idea would be kindly appreciated.

答案1

得分: 2

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

英文:

Looks like you haven't copied node_modules from build step. Add this like to your code

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

huangapple
  • 本文由 发表于 2023年5月30日 01:49:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/76359393.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定