如何创建Angular Universal Dockerfile和docker-compose.yml文件?

时间:2020-04-19 20:34:27

标签: angular docker dockerfile containers angular-universal

因此,我正在尝试对Angular通用应用程序进行Docker化,以将其与已经实现docker化的spring boot rest后端一起使用,我发现很难在互联网上获得一些帮助和资源。这里有一个Angular通用Dockerfile的示例,但是尽管我做了更改,但它对我却不起作用。

FROM node:12-alpine as buildContainer
WORKDIR /app
COPY ./package.json ./package-lock.json /app/
RUN npm install
COPY . /app
RUN npm run build:universal
# RUN npm run generate:pretender
RUN npm run test:ssr

FROM node:8-alpine

WORKDIR /app
# Copy dependency definitions
COPY --from=buildContainer /app/package.json /app
# COPY --from=buildContainer /app/server.js /app

# Get all the code needed to run the app
COPY --from=buildContainer /app/dist /app/dist
# COPY --from=buildContainer /app/static /app/static
COPY --from=buildContainer /app/dist-server /app/dist-server

# Expose the port the app runs in
EXPOSE 4200

# Serve the app
CMD ["npm", "run", "server"]

我正在使用以下命令docker build -t angular-universal .构建映像,但是在第六步说

失败

npm错误!缺少脚本:build:universal npm ERR!可以在以下位置找到此运行的完整日志: npm ERR! /root/.npm/_logs/2020-04-19T20_29_16_368Z-debug.log 命令'/ bin / sh -c npm run build:universal'返回非零代码:1

有人有同样的问题吗?

这是我的package.json

{
"name": "jobnow",
  "version": "0.0.0",
  "scripts": {
    "ng": "ng",
    "start": "ng serve --host 0.0.0.0",
    "build": "ng build --prod",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e",
    "deploy": "ng deploy --base-href=https://samgevorgyan.github.io/jobnow/"
  },
  "private": true,
  "dependencies": {
    "@angular/animations": "~9.0.1",
    "@angular/cdk": "^9.0.0",
    "@angular/common": "~9.0.1",
    "@angular/compiler": "~9.0.1",
    "@angular/core": "~9.0.1",
    "@angular/flex-layout": "^9.0.0-beta.29",
    "@angular/forms": "~9.0.1",
    "@angular/material": "^9.0.0",
    "@angular/material-moment-adapter": "^9.1.2",
    "@angular/platform-browser": "~9.0.1",
    "@angular/platform-browser-dynamic": "~9.0.1",
    "@angular/router": "~9.0.1",
    "@capacitor/android": "^1.5.0",
    "@capacitor/angular": "^1.0.2",
    "@capacitor/core": "latest",
    "@fortawesome/angular-fontawesome": "^0.6.1",
    "@fortawesome/fontawesome-svg-core": "^1.2.28",
    "@fortawesome/free-brands-svg-icons": "^5.13.0",
    "@fortawesome/free-regular-svg-icons": "^5.13.0",
    "@fortawesome/free-solid-svg-icons": "^5.13.0",
    "@gilsdav/ngx-translate-router": "^3.0.0",
    "@gilsdav/ngx-translate-router-http-loader": "^1.1.0",
    "@ngrx/effects": "^8.6.0",
    "@ngrx/store": "^8.6.0",
    "@ngrx/store-devtools": "^8.6.0",
    "@ngx-translate/core": "^12.1.2",
    "@ngx-translate/http-loader": "^4.0.0",
    "@types/googlemaps": "^3.39.3",
    "angular-cli-ghpages": "^0.6.2",
    "crypto-ts": "^1.0.2",
    "moment": "^2.24.0",
    "ngx-cookie-service": "^3.0.3",
    "ngx-image-cropper": "^3.1.6",
    "ngx-toastr": "^12.0.0",
    "rxjs": "~6.5.4",
    "tslib": "^1.10.0",
    "zone.js": "~0.10.2"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "^0.901.0",
    "@angular/cli": "^9.1.0",
    "@angular/compiler-cli": "~9.0.1",
    "@angular/language-service": "~9.0.1",
    "@capacitor/cli": "latest",
    "@types/jasmine": "~3.5.4",
    "@types/jasminewd2": "~2.0.8",
    "@types/node": "~13.7.1",
    "codelyzer": "^5.2.1",
    "jasmine-core": "~3.5.0",
    "jasmine-spec-reporter": "~4.2.1",
    "karma": "^5.0.1",
    "karma-chrome-launcher": "~3.1.0",
    "karma-coverage-istanbul-reporter": "~2.1.1",
    "karma-jasmine": "~3.1.1",
    "karma-jasmine-html-reporter": "^1.5.2",
    "protractor": "~5.4.3",
    "ts-node": "~8.6.2",
    "tslint": "^6.1.1",
    "typescript": "^3.7.5"
  }
}

1 个答案:

答案 0 :(得分:0)

可能有点晚,但在您的 Dockerfile 中:

<块引用>

运行 npm run build:universal

你需要在你的 package.json 中有一个这样的脚本:

"build:universal": "ng build --prod && ng run jobnow:server:production"

还有这一行:

<块引用>

运行 npm run test:ssr

您可以删除它或添加另一个脚本,如下所示:

"test:ssr": "run-p test:ssr:*"