[英]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.