【问题标题】:Docker image build getting stuck at "npm install" for ReactJSDocker 映像构建卡在 ReactJS 的“npm install”
【发布时间】:2020-10-14 19:49:25
【问题描述】:

我正在为我的微服务应用程序创建一个 kubernetes 集群。 我正在构建 docker 映像,但由于某种原因,它卡住了@npm install step:

卡在这里:

$ docker build -t karanshreds/client .
Sending build context to Docker daemon  626.2kB
Step 1/7 : FROM node:alpine
 ---> 3bf5a7d41d77
Step 2/7 : ENV CI=true
 ---> Running in 3ffe706d12a3
Removing intermediate container 3ffe706d12a3
 ---> bcd186e89d1b
Step 3/7 : WORKDIR /app
 ---> Running in 4b68ea73ef58
emoving intermediate container 4b68ea73ef58
 ---> 2427bc0ae6e8
Step 4/7 : COPY package.json ./
 ---> 2d26f309fb4d
Step 5/7 : RUN npm install
 ---> Running in ce5043208676
npm WARN deprecated urix@0.1.0: Please see https://github.com/lydell/urix#deprecated
npm WARN deprecated @types/testing-library__dom@7.5.0: This is a stub types definition. testing-library__dom provides its own type definitions, so you do not need this installed.
npm WARN deprecated resolve-url@0.2.1: https://github.com/lydell/resolve-url#deprecated
npm WARN deprecated chokidar@2.1.8: Chokidar 2 will break on node v14+. Upgrade to chokidar 3 with 15x less dependencies.
npm WARN deprecated request@2.88.2: request has been deprecated, see https://github.com/request/request/issues/3142
npm WARN deprecated fsevents@1.2.13: fsevents 1 will break on node v14+ and could be using insecure binaries. Upgrade to fsevents 2.
npm WARN deprecated left-pad@1.3.0: use String.prototype.padStart()
npm WARN deprecated core-js@2.6.11: core-js@<3 is no longer maintained and not recommended for usage due to the number of issues. Please, upgrade your dependencies to the actual version of core-js@3.

Dockerfile

FROM node:alpine
ENV CI=true
WORKDIR /app
COPY package.json ./
RUN npm install
COPY ./ ./ 
CMD ["npm", "start"]

当我使用 node 而不是 node:alpine 时它可以工作。 但它会创建大小为 1GB+ 的图像。这显然是我不想要的。

#NOTE:以防万一您想知道我的 package.json 文件有什么:

{
  "name": "client",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@testing-library/jest-dom": "^4.2.4",
    "@testing-library/react": "^9.5.0",
    "@testing-library/user-event": "^7.2.1",
    "axios": "^0.19.2",
    "react": "^16.13.1",
    "react-dom": "^16.13.1",
    "react-scripts": "3.4.1"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": "react-app"
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  }
}

【问题讨论】:

  • 你说你得到一个错误,但我在 docker build 命令的日志中没有看到任何错误,只是一堆关于某些包被弃用的警告,你也应该尝试使用多阶段 docker 构建并实际构建反应应用程序而不是使用开发版本
  • 我刚刚编辑了它。你能帮忙吗?

标签: node.js reactjs docker npm kubernetes


【解决方案1】:

我不确定运行npm install 命令的问题是什么,日志似乎没有理由相信它们实际上有问题,我所看到的只是一些关于某些软件包被弃用的警告消息,但是实际上不是错误。不管怎样,你说当你使用node而不是node:alpine时它确实有效,这可以帮助我们。

通常你想要的是:构建应用程序并让它在单独的 docker 层中运行。您可以为此目的使用多阶段 docker build。这样,您将拥有一个单独的环境,您可以在其中构建您的应用程序(node 根据需要)并单独运行它,比如说最小的 nginx。您可以在下面看到一个支持该概念的单个 Dockerfile 示例

# build environment
FROM node as build
WORKDIR /app
COPY package.json ./
COPY package-lock.json ./
RUN npm install
COPY . ./
RUN npm run build

# production environment
FROM nginx:stable-alpine
COPY --from=build /app/build /usr/share/nginx/html
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]

【讨论】:

  • 谢谢,这个建得真快。
猜你喜欢
  • 2020-06-15
  • 1970-01-01
  • 1970-01-01
  • 2021-05-08
  • 1970-01-01
  • 2021-02-14
  • 1970-01-01
  • 1970-01-01
  • 2021-12-14
相关资源
最近更新 更多