首页 > 解决方案 > 使用 docker multistage 构建 vue.js 应用程序在 COPY 上失败

问题描述

我想使用 docker 部署的 vue.js 应用程序有问题。使用此版本的 docker 配置文件,部署一直运行良好,但自从在应用程序上导入大文件后,我遇到了一个众所周知的错误。我发现了多个具有相同问题的主题,但没有适合我的解决方案。


包.json

{
  "name": "[...]",
  "version": "0.1.0",
  "private": true,
  "scripts": {
    "serve": "vue-cli-service serve",
    "build": "vue-cli-service build",
    "lint": "vue-cli-service lint"
  },
[...]
}

Dockerfile

#build stage
FROM node:lts-alpine as build-stage
WORKDIR /app
ENV PATH /app/node_modules/.bin:$PATH
COPY package*.json ./
RUN npm install
COPY . .
RUN npm run build

# production stage
FROM nginx:stable-alpine as production-stage 
COPY ./nginx_config/nginx.conf /etc/nginx/nginx.conf

RUN rm -rf /usr/share/nginx/html/*
# RUN ls -lart

COPY --from=build-stage /app/dist /usr/share/nginx/html 

EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]

Docker-compose.yml

version: '3.7'

services:
  portfolio:
    container_name: portfolio
    build:
      context: .
      dockerfile: Dockerfile
    ports:
      - '80:80'

建筑日志

    Building portfolio
Step 1/13 : FROM node:lts-alpine as build-stage
 ---> 8e69714aa82b
Step 2/13 : WORKDIR /app
 ---> Using cache
 ---> c1e93cbee6d6
Step 3/13 : ENV PATH /app/node_modules/.bin:$PATH
 ---> Using cache
 ---> 23f0528e6c9f
Step 4/13 : COPY package*.json ./
 ---> 03789a2585f2
Step 5/13 : RUN npm install
 ---> Running in a7f62007da83
npm WARN deprecated babel-eslint@10.1.0: babel-eslint is now @babel/eslint-parser. This package will no longer receive updates.
npm WARN deprecated vue-cli@2.9.6: This package has been deprecated in favour of @vue/cli
npm WARN deprecated html-webpack-plugin@3.2.0: 3.x is no longer supported
npm WARN deprecated eslint-loader@2.2.1: This loader has been deprecated. Please use eslint-webpack-plugin
npm WARN deprecated request@2.88.2: request has been deprecated, see https://github.com/request/request/issues/3142
npm WARN deprecated request-promise-native@1.0.9: request-promise-native has been deprecated because it extends the now deprecated request package, see https://github.com/request/request/issues/3142
npm WARN deprecated @hapi/joi@15.1.1: Switch to 'npm install joi'
npm WARN deprecated coffee-script@1.12.7: CoffeeScript on NPM has moved to "coffeescript" (no hyphen)
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 har-validator@5.1.5: this library is no longer supported
npm WARN deprecated uuid@3.4.0: Please upgrade  to version 7 or higher.  Older versions may use Math.random() in certain circumstances, which is known to be problematic.  See https://v8.dev/blog/math-random for details.
npm WARN deprecated request@2.16.6: request has been deprecated, see https://github.com/request/request/issues/3142
npm WARN deprecated core-js@2.6.12: core-js@<3.3 is no longer maintained and not recommended for usage due to the number of issues. Because of the V8 engine whims, feature detection in old core-js versions could cause a slowdown up to 100x even if nothing is polyfilled. Please, upgrade your dependencies to the actual version of core-js.
npm WARN deprecated @hapi/bourne@1.3.2: This version has been deprecated and is no longer supported or maintained
npm WARN deprecated @hapi/address@2.1.4: Moved to 'npm install @sideway/address'
npm WARN deprecated @hapi/topo@3.1.6: This version has been deprecated and is no longer supported or maintained
npm WARN deprecated @hapi/hoek@8.5.1: This version has been deprecated and is no longer supported or maintained
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 querystring@0.2.0: The querystring API is considered Legacy. new code should use the URLSearchParams API instead.
npm WARN deprecated node-uuid@1.4.8: Use uuid module instead
npm WARN deprecated hawk@0.10.2: This module moved to @hapi/hawk. Please make sure to switch over as this distribution is no longer supported and may contain bugs and critical security issues.
npm WARN deprecated svgo@1.3.2: This SVGO version is no longer supported. Upgrade to v2.x.x.
npm WARN deprecated boom@0.3.8: This version has been deprecated in accordance with the hapi support policy (hapi.im/support). Please upgrade to the latest version to get the best features, bug fixes, and security patches. If you are unable to upgrade at this time, paid support is available for older versions (hapi.im/commercial).
npm WARN deprecated hoek@0.7.6: This version has been deprecated in accordance with the hapi support policy (hapi.im/support). Please upgrade to the latest version to get the best features, bug fixes, and security patches. If you are unable to upgrade at this time, paid support is available for older versions (hapi.im/commercial).
npm WARN deprecated cryptiles@0.1.3: This version has been deprecated in accordance with the hapi support policy (hapi.im/support). Please upgrade to the latest version to get the best features, bug fixes, and security patches. If you are unable to upgrade at this time, paid support is available for older versions (hapi.im/commercial).
npm WARN deprecated sntp@0.1.4: This module moved to @hapi/sntp. Please make sure to switch over as this distribution is no longer supported and may contain bugs and critical security issues.
npm WARN deprecated json3@3.2.6: Please use the native JSON object instead of JSON 3
npm WARN deprecated urix@0.1.0: Please see https://github.com/lydell/urix#deprecated
npm WARN deprecated resolve-url@0.2.1: https://github.com/lydell/resolve-url#deprecated
npm WARN deprecated node-uuid@1.4.1: Use uuid module instead

> ws@0.4.31 install /app/node_modules/tomahawk/node_modules/socket.io/node_modules/engine.io/node_modules/ws
> (node-gyp rebuild 2> builderror.log) || (exit 0)


> ws@0.4.31 install /app/node_modules/tomahawk/node_modules/socket.io/node_modules/socket.io-client/node_modules/engine.io-client/node_modules/ws
> (node-gyp rebuild 2> builderror.log) || (exit 0)


> yorkie@2.0.0 install /app/node_modules/yorkie
> node bin/install.js

setting up Git hooks
can't find .git directory, skipping Git hooks installation

> node-sass@5.0.0 install /app/node_modules/node-sass
> node scripts/install.js

Downloading binary from https://github.com/sass/node-sass/releases/download/v5.0.0/linux_musl-x64-83_binding.node
Download complete
Binary saved to /app/node_modules/node-sass/vendor/linux_musl-x64-83/binding.node
Caching binary to /root/.npm/node-sass/5.0.0/linux_musl-x64-83_binding.node

> @fortawesome/fontawesome-common-types@0.2.36 postinstall /app/node_modules/@fortawesome/fontawesome-common-types
> node attribution.js

Font Awesome Free 0.2.36 by @fontawesome - https://fontawesome.com
License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License)


> core-js@3.19.1 postinstall /app/node_modules/core-js
> node -e "try{require('./postinstall')}catch(e){}"

Thank you for using core-js ( https://github.com/zloirock/core-js ) for polyfilling JavaScript standard library!

The project needs your help! Please consider supporting of core-js:
> https://opencollective.com/core-js
> https://patreon.com/zloirock
> https://paypal.me/zloirock
> bitcoin: bc1qlea7544qtsmj2rayg0lthvza9fau63ux0fstcz

Also, the author of core-js ( https://github.com/zloirock ) is looking for a good job -)


> ejs@2.7.4 postinstall /app/node_modules/ejs
> node ./postinstall.js

Thank you for installing EJS: built with the Jake JavaScript build tool (https://jakejs.com/)


> core-js@2.6.12 postinstall /app/node_modules/raf-manager/node_modules/core-js
> node -e "try{require('./postinstall')}catch(e){}"

Thank you for using core-js ( https://github.com/zloirock/core-js ) for polyfilling JavaScript standard library!

The project needs your help! Please consider supporting of core-js on Open Collective or Patreon:
> https://opencollective.com/core-js
> https://www.patreon.com/zloirock

Also, the author of core-js ( https://github.com/zloirock ) is looking for a good job -)


> @fortawesome/free-brands-svg-icons@5.15.4 postinstall /app/node_modules/@fortawesome/free-brands-svg-icons
> node attribution.js

Font Awesome Free 5.15.4 by @fontawesome - https://fontawesome.com
License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License)


> @fortawesome/free-solid-svg-icons@5.15.4 postinstall /app/node_modules/@fortawesome/free-solid-svg-icons
> node attribution.js

Font Awesome Free 5.15.4 by @fontawesome - https://fontawesome.com
License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License)


> node-sass@5.0.0 postinstall /app/node_modules/node-sass
> node scripts/build.js

Binary found at /app/node_modules/node-sass/vendor/linux_musl-x64-83/binding.node
Testing binary
Binary is fine
npm notice created a lockfile as package-lock.json. You should commit this file.
npm WARN notsup Unsupported engine for dns@0.1.5: wanted: {"node":">= 0.10.0 < 0.11.0"} (current: {"node":"14.17.0","npm":"6.14.13"})
npm WARN notsup Not compatible with your version of node/npm: dns@0.1.5
npm WARN notsup Unsupported engine for tomahawk@0.1.5: wanted: {"node":">= 0.8.0 < 0.11.0"} (current: {"node":"14.17.0","npm":"6.14.13"})
npm WARN notsup Not compatible with your version of node/npm: tomahawk@0.1.5
npm WARN notsup Unsupported engine for hawk@0.10.2: wanted: {"node":"0.8.x"} (current: {"node":"14.17.0","npm":"6.14.13"})
npm WARN notsup Not compatible with your version of node/npm: hawk@0.10.2
npm WARN notsup Unsupported engine for hoek@0.7.6: wanted: {"node":"0.8.x"} (current: {"node":"14.17.0","npm":"6.14.13"})
npm WARN notsup Not compatible with your version of node/npm: hoek@0.7.6
npm WARN notsup Unsupported engine for boom@0.3.8: wanted: {"node":"0.8.x"} (current: {"node":"14.17.0","npm":"6.14.13"})
npm WARN notsup Not compatible with your version of node/npm: boom@0.3.8
npm WARN notsup Unsupported engine for cryptiles@0.1.3: wanted: {"node":"0.8.x"} (current: {"node":"14.17.0","npm":"6.14.13"})
npm WARN notsup Not compatible with your version of node/npm: cryptiles@0.1.3
npm WARN notsup Unsupported engine for sntp@0.1.4: wanted: {"node":"0.8.x"} (current: {"node":"14.17.0","npm":"6.14.13"})
npm WARN notsup Not compatible with your version of node/npm: sntp@0.1.4
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@~2.3.2 (node_modules/chokidar/node_modules/fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@2.3.2: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@^1.2.7 (node_modules/watchpack-chokidar2/node_modules/chokidar/node_modules/fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.2.13: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@^1.2.7 (node_modules/webpack-dev-server/node_modules/chokidar/node_modules/fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.2.13: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})
npm WARN @fortawesome/vue-fontawesome@2.0.6 requires a peer of @fortawesome/fontawesome-svg-core@~1 || >=1.3.0-beta1 but none is installed. You must install peer dependencies yourself.
npm WARN request-promise-native@1.0.9 requires a peer of request@^2.34 but none is installed. You must install peer dependencies yourself.
npm WARN request-promise-core@1.1.4 requires a peer of request@^2.34 but none is installed. You must install peer dependencies yourself.

added 1815 packages from 726 contributors and audited 1819 packages in 64.473s

95 packages are looking for funding
  run `npm fund` for details

found 92 vulnerabilities (14 low, 35 moderate, 41 high, 2 critical)
  run `npm audit fix` to fix them, or `npm audit` for details
Removing intermediate container a7f62007da83
 ---> 6d342fed97bc
Step 6/13 : COPY . .
 ---> 11ec260a395b
Step 7/13 : RUN npm run build
 ---> Running in 90b0c3e10671

> portfolio-vpoujade@0.1.0 build /app
> vue-cli-service build


-  Building for production...
Removing intermediate container 90b0c3e10671
 ---> 1635da3cea2f

Step 8/13 : FROM nginx:stable-alpine as production-stage
 ---> e1ccef1fb908
Step 9/13 : COPY ./nginx_config/nginx.conf /etc/nginx/nginx.conf
 ---> Using cache
 ---> d4839c3fabf3
Step 10/13 : RUN rm -rf /usr/share/nginx/html/*
 ---> Using cache
 ---> f78f258f926c
Step 11/13 : COPY --from=build-stage /app/dist /usr/share/nginx/html
ERROR: Service 'portfolio' failed to build: COPY failed: stat app/dist: file does not exist

当我使用docker-compose up --build -d命令部署应用程序时,在第 12 步:

COPY --from=build-stage /app/dist/ /usr/share/nginx/html

,我得到错误

ERROR: Service 'portfolio' failed to build: COPY failed: stat app/dist: file does not exist

先感谢您!

标签: dockervue.jsnginxdocker-composebuild

解决方案


推荐阅读