[英]npm ERR! Failed when make ionic serve command (ask for checking version of node.js and npm installed)
我正在尝试使用ionic框架2创建一个docker环境(我希望与git和我的团队一起使用)。
我有一个名为ionic-boilerplate的项目目录。 在此目录中,我有一个没有node_modules文件夹的离子应用程序。 当我执行docker-compose up --build命令时,在我的docker中安装所需的所有组件。
这是我的ionic-boilerplate / Dockerfile
:
FROM node:6.9.4
RUN npm install -g cordova@4.2.0 ionic@2.2.1
ENV DOCKER_CONTAINER_APP=/web-app
RUN mkdir -p $DOCKER_CONTAINER_APP
ADD . $DOCKER_CONTAINER_APP
RUN cd $DOCKER_CONTAINER_APP
WORKDIR $DOCKER_CONTAINER_APP
RUN npm install
EXPOSE 8100 35729
CMD ionic serve --all
这是我的ionic-boilerplate / docker-compose.yml
:
version: '2'
services:
ionic:
build: .
ports:
- "8100:8100"
- "35729:35729"
volumes:
- .:/web-app
- ./node_modules:/web-app/node_modules
当我启动命令docker-compose up --build
或docker-compose run ionic
,出现以下错误:
Attaching to test_ionic_1
ionic_1 | npm info it worked if it ends with ok
ionic_1 | npm info using npm@3.10.10
ionic_1 | npm info using node@v6.9.4
ionic_1 | npm info lifecycle ionic-hello-world@~preionic:serve: ionic-hello-world@
ionic_1 | npm info lifecycle ionic-hello-world@~ionic:serve: ionic-hello-world@
ionic_1 |
ionic_1 | > ionic-hello-world@ ionic:serve /web-app
ionic_1 | > ionic-app-scripts serve "--all" "--v2" "--address" "0.0.0.0" "--port" "8100" "--livereload-port" "35729"
ionic_1 |
ionic_1 | sh: 1: ionic-app-scripts: not found
ionic_1 |
ionic_1 | npm info lifecycle ionic-hello-world@~ionic:serve: Failed to exec ionic:serve script
ionic_1 | npm ERR! Linux 4.4.0-59-generic
ionic_1 | npm ERR! argv "/usr/local/bin/node" "/usr/local/bin/npm" "run" "ionic:serve" "--" "--all" "--v2" "--address" "0.0.0.0" "--port" "8100" "--livereload-port" "35729"
ionic_1 | npm ERR! node v6.9.4
ionic_1 | npm ERR! npm v3.10.10
ionic_1 | npm ERR! file sh
ionic_1 | npm ERR! code ELIFECYCLE
ionic_1 | npm ERR! errno ENOENT
ionic_1 | npm ERR! syscall spawn
ionic_1 | npm ERR! ionic-hello-world@ ionic:serve: `ionic-app-scripts serve "--all" "--v2" "--address" "0.0.0.0" "--port" "8100" "--livereload-port" "35729"`
ionic_1 | npm ERR! spawn ENOENT
ionic_1 | npm ERR!
ionic_1 | npm ERR! Failed at the ionic-hello-world@ ionic:serve script 'ionic-app-scripts serve "--all" "--v2" "--address" "0.0.0.0" "--port" "8100" "--livereload-port" "35729"'.
ionic_1 | npm ERR! Make sure you have the latest version of node.js and npm installed.
ionic_1 | npm ERR! If you do, this is most likely a problem with the ionic-hello-world package,
ionic_1 | npm ERR! not with npm itself.
ionic_1 | npm ERR! Tell the author that this fails on your system:
ionic_1 | npm ERR! ionic-app-scripts serve "--all" "--v2" "--address" "0.0.0.0" "--port" "8100" "--livereload-port" "35729"
ionic_1 | npm ERR! You can get information on how to open an issue for this project with:
ionic_1 | npm ERR! npm bugs ionic-hello-world
ionic_1 | npm ERR! Or if that isn't available, you can get their info via:
ionic_1 | npm ERR! npm owner ls ionic-hello-world
ionic_1 | npm ERR! There is likely additional logging output above.
ionic_1 |
ionic_1 | npm ERR! Please include the following file with any support request:
ionic_1 | npm ERR! /web-app/npm-debug.log
ionic_1 | There was an error serving your Ionic application: There was an error with the spawned command: serve
test_ionic_1 exited with code 0
该错误发生在Dockerfile
指令 CMD ionic serve
。
在没有docker的本地环境中使用相同的方法,即执行命令npm install
然后ionic serve
效果很好! docker方法在哪里出错?
您的图像是这样构建的:
ENV DOCKER_CONTAINER_APP=/web-app
WORKDIR $DOCKER_CONTAINER_APP
RUN npm install
但是在运行时,docker-compose会这样做:
volumes:
- .:/web-app
- ./node_modules:/web-app/node_modules
因为您正在/web-app
处安装外部路径,所以它将覆盖该路径中映像中内置的所有内容。 这在开发过程中可能很有用,但需要权衡取舍:您应该在外部提供路径上所需的任何内容,因为映像本身不再可以。
使用这些挂载可以有效地从Dockerfile中撤消以下步骤:
ADD . $DOCKER_CONTAINER_APP
RUN npm install
一种选择是在容器外部运行npm install
。 如果用所需的模块填充外部的node_modules/
,那么代码将很高兴并且可以运行。
另一个选择是从docker-compose.yml中删除卷挂载,并使用映像中已内置的代码。
这应该解决该问题,但代价是仅拥有图像中已经存在的内容,而无法快速更新它。 在这种情况下,每次更改代码时,都必须重建映像。
您还可以使用在容器启动时执行npm install
的entypoint脚本。
Dockerfile:
ADD entrypoint.sh /entrypoint.sh
ENTRYPOINT ["/entrypoint.sh"]
entrypoint.sh
#!/bin/sh
npm install
exec "$@"
这将在启动时运行npm install
,然后从Dockerfile执行CMD
。
这是权衡取舍的问题。 您现在拥有事物的方式是开发过程中的常见模式。 由于代码已安装在容器中,因此可以快速进行迭代。 您更改代码,可能必须快速重启容器,并且新代码正在运行。 无需重建。
但另一方面,您的应用所需的所有内容都必须在外部提供。
在开发IMO期间,这是一个有用的折衷,因为您可以提高速度。 当需要部署应用程序时,通常您将停止使用这些安装,并依赖于映像中的内容。 这样,事情就更容易预测,并且更少的依赖项/可能出错的事情。
入口点脚本是两全其美的方法,因为您可以保留装载,但仍可以更新依赖关系。 不过,在启动过程中确实要花费一些时间。
最后,我找到了另一种方式。 我删除了DockerFile中的RUN指令,然后像这样重写我的docker-compose.yml:
version: '2'
services:
web:
build:
context: .
environment:
- NODE_ENV=development
- DEBUG='true'
ports:
- 8100:8100
- 35729:35729
volumes:
- .:/web-app
- ./node_modules:/web-app/node_modules
command: sh -c 'npm install; ionic serve --all'
它像我期望的那样工作。 即安装npm软件包相关性并运行ionic serve命令。
不要犹豫,问我这不是一个好习惯。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.