Vue 2使用Docker构建 – sh: vue-cli-service: 未找到

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

Vue 2 build with docker - sh: vue-cli-service: not found

问题

我想用Docker构建一个带有Vue 2应用程序,但是遇到了sh: vue-cli-service: not found错误。
对于serve,我有另一个Docker文件,那里的一切都很好,但是在构建中我遇到了这个错误!

Dockerfile

  1. # 构建阶段
  2. FROM node:14.18.0-alpine as build-stage
  3. ENV NODE_ENV=production
  4. WORKDIR /var/www/html/app
  5. COPY package*.json ./
  6. RUN npm install -g @vue/cli
  7. RUN npm i
  8. COPY . .
  9. RUN npm run build
  10. # 生产阶段
  11. FROM nginx:stable-alpine as production-stage
  12. COPY --from=build-stage /app/dist /usr/share/nginx/html
  13. EXPOSE 80
  14. CMD ["nginx", "-g", "daemon off;"]

docker-compose.yml

  1. version: '3.7'
  2. services:
  3. app:
  4. build:
  5. context: .
  6. dockerfile: Dockerfile
  7. ports:
  8. - "8080:80"

命令:docker-compose up -d

英文:

I want to build a vue 2 app with docker, but got sh: vue-cli-service: not found error.
For serve, I have another docker files and everything there is fine, but in build I got this error!

Dockerfile

  1. # build stage
  2. FROM node:14.18.0-alpine as build-stage
  3. ENV NODE_ENV=production
  4. WORKDIR /var/www/html/app
  5. COPY package*.json ./
  6. RUN npm install -g @vue/cli
  7. RUN npm i
  8. COPY . .
  9. RUN npm run build
  10. # production stage
  11. FROM nginx:stable-alpine as production-stage
  12. COPY --from=build-stage /app/dist /usr/share/nginx/html
  13. EXPOSE 80
  14. CMD ["nginx", "-g", "daemon off;"]

docker-compose.yml

  1. version: '3.7'
  2. services:
  3. app:
  4. build:
  5. context: .
  6. dockerfile: Dockerfile
  7. ports:
  8. - "8080:80"

command: docker-compose up -d

答案1

得分: 1

你的docker-compose文件中的app服务是否使用了卷?

我在使用Vue.js 2和Docker时遇到了类似的错误,npm run serve失败,因为Docker容器中的node_modules存储库丢失了。造成这个问题的根本原因是我在开发环境中使用了绑定挂载,以实现代码的热重载,这导致Docker在运行npm install -g @vue/cli后覆盖了node_modules存储库,这就解释了cli找不到的原因。

我通过为node_modules存储库使用卷来解决了这个问题,这个答案中有详细说明

英文:

Are you using volumes for your app service in your docker-compose file?

I got a similar error on Vue.js 2 with Docker npm run serve was failing because the node_modules repository went missing in the Docker container. The root cause of it was that I am using bind mounts to allow code hot reload in my development environment, which made Docker overwrite the node_modules repository after running npm install -g @vue/cli, which explains that cli couldn't be found.

I solved this by using a volume for the node_modules repository, as explained by this answer.

huangapple
  • 本文由 发表于 2023年7月27日 16:26:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76777850.html
匿名

发表评论

匿名网友

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

确定