我正在为我的微服务应用程序制作一个kubernetes集群。 我正在构建docker映像,但由于某种原因,它陷入了@npm安装步骤:
卡在这里:
$ 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+。 这显然是我不想要的。
#注意:以防万一如果您想知道我的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"
]
}
}
我不确定运行npm install
命令的问题是什么,日志似乎没有提供任何理由让人相信其中确实存在错误,我看到的只是一些警告消息,说明某些软件包正在被弃用,但实际上并不是错误。 无论哪种方式,您都说当您使用node
而不是node:alpine
时它确实工作了,这可以帮助我们。
通常你想要的是:构建应用程序,并让它在单独的docker层中运行。 您可以为此使用多阶段docker构建。 这样您就有了一个单独的环境,在这个环境中您可以构建您的应用程序(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;"]