简体   繁体   English

如何在Docker映像创建过程的npm-install过程中排除@ angular / cli?

[英]How can I exclude @angular/cli from npm-install process in a Docker image creation process?

I'm starting a project that will use Angular.js and Node.js and it will be inside a Docker container. 我正在启动一个将使用Angular.jsNode.js的项目,该项目将在Docker容器内。 In my Dockerfile I've indicate that Docker must RUN npm install to configure my project when it will build the Docker image. 在我的Dockerfile我已指示Docker在构建Docker映像时必须RUN npm install来配置我的项目。 This is a part of build logs: 这是构建日志的一部分:

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

I have installed @angular/cli package with npm in my OS and I don't want that the package @angular/cli will be installed in my project when Docker RUN npm install , it makes heavier my Docker image. 我已经在操作系统中使用npm安装了@angular/cli软件包,并且我不希望在Docker RUN npm install时将@angular/cli软件包安装在我的项目中,这会使我的Docker映像变重。

How can I exclude @angular/cli from npm-install process in a Docker image creation process? 如何在Docker映像创建过程的npm-install过程中排除@ angular / cli?

To solve that I'm using this code in my Dockerfile : 为了解决这个问题,我在Dockerfile使用了以下代码:

RUN npm uninstall @angular/cli

But it didn't solve my problem. 但这并不能解决我的问题。 What you can suggest me? 你能建议我什么?

One of the comments talks about running ng-build and then using the dist folder in your Dockerfile. 其中一条评论谈到了运行ng-build ,然后使用Dockerfile中的dist文件夹。 This is a step you can easily automate in your build 这是您可以轻松地自动构建的步骤

This is the perfect opportunity to make use of Docker's multi-stage builds . 这是利用Docker 多阶段构建的绝佳机会。

You're going to want your Dockerfile to look something like this: 您将希望您的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

Here is the multi-stage part: 这是多阶段的部分:

FROM nginx:alpine  # or whatever image you want here
COPY --from=build /app/dist/ /usr/share/nginx/html/

Now you have a nice, small image, with only your dist directory, and none of the unnecessary overhead from running ng-build and npm-install in your final container. 现在,您有了一个漂亮的小型映像,仅包含dist目录,而没有在最终容器中运行ng-buildnpm-install的不必要开销。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM