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

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

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

问题

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

Dockerfile

# 构建阶段
FROM node:14.18.0-alpine as build-stage
ENV NODE_ENV=production
WORKDIR /var/www/html/app
COPY package*.json ./
RUN npm install -g @vue/cli
RUN npm i

COPY . .
RUN npm run build

# 生产阶段
FROM nginx:stable-alpine as production-stage
COPY --from=build-stage /app/dist /usr/share/nginx/html
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]

docker-compose.yml

version: '3.7'
services:
  app:
    build:
      context: .
      dockerfile: Dockerfile
    ports:
      - "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

# build stage
FROM node:14.18.0-alpine as build-stage
ENV NODE_ENV=production
WORKDIR /var/www/html/app
COPY package*.json ./
RUN npm install -g @vue/cli
RUN npm i

COPY . .
RUN npm run build

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

docker-compose.yml

version: '3.7'
services:
  app:
    build:
      context: .
      dockerfile: Dockerfile
    ports:
      - "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:

确定