[英]How can I exclude @angular/cli from npm-install process in a Docker image creation process?
我正在启动一个将使用Angular.js和Node.js的项目,该项目将在Docker容器内。 在我的Dockerfile
我已指示Docker在构建Docker映像时必须RUN npm install
来配置我的项目。 这是构建日志的一部分:
Step 4/10 : RUN npm install
---> Running in 90d567c905d4
> @angular/cli@6.0.3 postinstall /usr/src/app/node_modules/@angular/cli
> node ./bin/ng-update-message.js
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@1.2.4 (node_modules/fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.2.4: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})
added 295 packages in 12.979s
我已经在操作系统中使用npm安装了@angular/cli
软件包,并且我不希望在Docker RUN npm install
时将@angular/cli
软件包安装在我的项目中,这会使我的Docker映像变重。
如何在Docker映像创建过程的npm-install过程中排除@ angular / cli?
为了解决这个问题,我在Dockerfile
使用了以下代码:
RUN npm uninstall @angular/cli
但这并不能解决我的问题。 你能建议我什么?
其中一条评论谈到了运行ng-build
,然后使用Dockerfile中的dist
文件夹。 这是您可以轻松地自动构建的步骤
这是利用Docker 多阶段构建的绝佳机会。
您将希望您的Dockerfile看起来像这样:
FROM node:7.9 as build
WORKDIR /app
COPY package.json /app
RUN npm install
COPY . /app/ # or wherever your source code is found
RUN ng-build
这是多阶段的部分:
FROM nginx:alpine # or whatever image you want here
COPY --from=build /app/dist/ /usr/share/nginx/html/
现在,您有了一个漂亮的小型映像,仅包含dist
目录,而没有在最终容器中运行ng-build
和npm-install
的不必要开销。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.