简体   繁体   English

如何将我的 angular 应用程序 dockerize 用于生产?

[英]How can I dockerize my angular app for production?

I try to containerize my existing angular app using docker this is my我尝试使用 docker 将我现有的 angular 应用程序容器化,这是我的

  • app name: admin-dashboard within the app I created my应用程序名称:我创建的应用程序中的admin-dashboard
  1. nginx.conf nginx.conf
  2. Dockerfile Dockerfile

I'm using Ubuntu 18.03, docker is running我正在使用 Ubuntu 18.03,docker 正在运行

This is my Dockerfile这是我的Dockerfile

### STAGE 1: Build ###
FROM node:12.22-alpine3.10 AS build
WORKDIR /usr/src/app
COPY package.json package-lock.json ./
RUN npm install -g @angular/cli
COPY . .
RUN ng build --prod
### STAGE 2: Run ###
FROM nginx:1.17.1-alpine
COPY nginx.conf /etc/nginx/nginx.conf
COPY --from=build /usr/src/app/dist/* /usr/share/nginx/html

This is my nginx.conf file这是我的nginx.conf文件

events{}
http {
    include /etc/nginx/mime.types;
    server {
        listen 80;
        server_name localhost;
        root /usr/share/nginx/html;
        index index.html;
        location / {
            try_files $uri $uri/ /index.html;
        }
    }
}

This is my package.json这是我的package.json

    {
  "name": "next-v8.1.2-lite",
  "version": "0.0.0",
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e"
  },
  "private": true,
  "dependencies": {
    "@angular/animations": "~8.1.2",
    "@angular/cdk": "^8.2.3",
    "@angular/common": "~8.1.2",
    "@angular/compiler": "~8.1.2",
    "@angular/core": "~8.1.2",
    "@angular/forms": "~8.1.2",
    "@angular/material": "^8.2.3",
    "@angular/platform-browser": "~8.1.2",
    "@angular/platform-browser-dynamic": "~8.1.2",
    "@angular/router": "~8.1.2",
    "@ng-bootstrap/ng-bootstrap": "^4.2.1",
    "apexcharts": "^3.8.0",
    "bootstrap": "^4.3.1",
    "css-animator": "^2.3.1",
    "jquery": "^3.4.1",
    "moment": "^2.24.0",
    "ng-click-outside": "^4.0.0",
    "ng2-validation": "^4.2.0",
    "ngx-perfect-scrollbar": "^8.0.0",
    "rxjs": "^6.5.2",
    "rxjs-compat": "^6.5.2",
    "screenfull": "^4.2.0",
    "tslib": "^1.9.0",
    "zone.js": "~0.9.1"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "^0.1102.9",
    "@angular/cli": "^11.2.9",
    "@angular/compiler-cli": "~8.1.2",
    "@angular/language-service": "~8.1.2",
    "@types/jasmine": "~3.3.8",
    "@types/jasminewd2": "~2.0.3",
    "@types/jquery": "^3.3.30",
    "@types/node": "~8.9.4",
    "codelyzer": "^5.0.0",
    "jasmine-core": "~3.4.0",
    "jasmine-spec-reporter": "~4.2.1",
    "karma": "^6.3.2",
    "karma-chrome-launcher": "~2.2.0",
    "karma-coverage-istanbul-reporter": "~2.0.1",
    "karma-jasmine": "~2.0.1",
    "karma-jasmine-html-reporter": "^1.4.0",
    "node-sass": "^4.12.0",
    "protractor": "~5.4.0",
    "ts-node": "~7.0.0",
    "tslint": "~5.15.0",
    "typescript": "~3.4.3"
  }
}

Finally I try to create my docker container with the command最后我尝试使用命令创建我的 docker 容器

docker build -t myapp:prod .

I'm getting this error我收到此错误

Step 6/9 : RUN ng build --prod
 ---> Running in c62fc8500224
An unhandled exception occurred: Cannot find module '@angular-devkit/build-angular/package.json'
Require stack:
- /usr/local/lib/node_modules/@angular/cli/node_modules/@angular-devkit/architect/node/node-modules-architect-host.js
- /usr/local/lib/node_modules/@angular/cli/node_modules/@angular-devkit/architect/node/index.js
- /usr/local/lib/node_modules/@angular/cli/models/architect-command.js
- /usr/local/lib/node_modules/@angular/cli/commands/build-impl.js
- /usr/local/lib/node_modules/@angular/cli/node_modules/@angular-devkit/schematics/tools/export-ref.js
- /usr/local/lib/node_modules/@angular/cli/node_modules/@angular-devkit/schematics/tools/index.js
- /usr/local/lib/node_modules/@angular/cli/utilities/json-schema.js
- /usr/local/lib/node_modules/@angular/cli/models/command-runner.js
- /usr/local/lib/node_modules/@angular/cli/lib/cli/index.js
- /usr/local/lib/node_modules/@angular/cli/lib/init.js
- /usr/local/lib/node_modules/@angular/cli/bin/ng
See "/tmp/ng-NjNJAp/angular-errors.log" for further details.
The command '/bin/sh -c ng build --prod' returned a non-zero code: 127

What's the issue, I'm not sure, I'm stuck.什么问题,我不确定,我被卡住了。

Try RUN npm i after the RUN ng build --prod.在 RUN ng build --prod 之后尝试 RUN npm i。

eg:例如:

    ### STAGE 1: Build ###
    FROM node:12.22-alpine3.10 AS build
    WORKDIR /usr/src/app
    COPY package.json package-lock.json ./
    RUN npm i -g @angular/cli
    
    # Install app dependencies:
    RUN npm i 
    
    COPY . .
    RUN ng build --prod
    ### STAGE 2: Run ###
    FROM nginx:1.17.1-alpine
    COPY nginx.conf /etc/nginx/nginx.conf
    COPY --from=build /usr/src/app/dist/* /usr/share/nginx/html

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

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