英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论