【问题标题】:Dockerizing Angular app: The /app/src/environments/environment.prod.ts path in file replacements does not existDockerizing Angular 应用程序:文件替换中的 /app/src/environments/environment.prod.ts 路径不存在
【发布时间】:2021-10-19 22:22:51
【问题描述】:

我正在尝试对 Angular 应用程序进行 dockerize。下面是dockerfile:

FROM node:alpine AS build
ENV NODE_ENV=development

WORKDIR /app

COPY package*.json ./

RUN npm install

COPY *.json ./
COPY *.js ./
COPY src .

RUN npm run build

FROM nginx:alpine

WORKDIR /usr/share/nginx/html

COPY --from=build /app/dist .

下面是angular.json配置:

{
  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "cli": {
    "analytics": false
  },
  "version": 1,
  "newProjectRoot": "projects",
  "projects": {
    "web": {
      "projectType": "application",
      "schematics": {
        "@schematics/angular:component": {
          "style": "scss"
        },
        "@schematics/angular:application": {
          "strict": true
        }
      },
      "root": "",
      "sourceRoot": "src",
      "prefix": "app",
      "architect": {
        "build": {
          "builder": "@angular-devkit/build-angular:browser",
          "options": {
            "outputPath": "dist",
            "index": "src/index.html",
            "main": "src/main.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "tsconfig.app.json",
            "inlineStyleLanguage": "scss",
            "assets": [
              "src/favicon.ico",
              "src/assets"
            ],
            "styles": [
              "src/styles.scss"
            ],
            "scripts": []
          },
          "configurations": {
            "production": {
              "budgets": [
                {
                  "type": "initial",
                  "maximumWarning": "500kb",
                  "maximumError": "1mb"
                },
                {
                  "type": "anyComponentStyle",
                  "maximumWarning": "2kb",
                  "maximumError": "4kb"
                }
              ],
              "fileReplacements": [
                {
                  "replace": "src/environments/environment.ts",
                  "with": "src/environments/environment.prod.ts"
                }
              ],
              "outputHashing": "all"
            },
            "development": {
              "buildOptimizer": false,
              "optimization": false,
              "vendorChunk": true,
              "extractLicenses": false,
              "sourceMap": true,
              "namedChunks": true
            }
          },
          "defaultConfiguration": "production"
        },
        "serve": {
          "builder": "@angular-devkit/build-angular:dev-server",
          "configurations": {
            "production": {
              "browserTarget": "web:build:production"
            },
            "development": {
              "browserTarget": "web:build:development"
            }
          },
          "defaultConfiguration": "development"
        },
        "extract-i18n": {
          "builder": "@angular-devkit/build-angular:extract-i18n",
          "options": {
            "browserTarget": "web:build"
          }
        },
        "test": {
          "builder": "@angular-devkit/build-angular:karma",
          "options": {
            "main": "src/test.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "tsconfig.spec.json",
            "karmaConfig": "karma.conf.js",
            "inlineStyleLanguage": "scss",
            "assets": [
              "src/favicon.ico",
              "src/assets"
            ],
            "styles": [
              "src/styles.scss"
            ],
            "scripts": []
          }
        }
      }
    }
  },
  "defaultProject": "web"
}

它在第 9 步引发以下错误的原因是什么?我试图在互联网上寻找这个错误,并找到了几个资源(thisthis),但这些对我没有任何帮助。

Warning: Support was requested for IE 11 in the project's browserslist configuration. IE 11 support is deprecated since Angular v12.
For more information, see https://angular.io/guide/browser-support
An unhandled exception occurred: The /app/src/environments/environment.prod.ts path in file replacements does not exist.
See "/tmp/ng-PPmHPI/angular-errors.log" for further details.
unable to stream build output: The command '/bin/sh -c npm run build' returned a non-zero code: 127. Please fix the Dockerfile and try again..

该错误似乎很容易解释,但请注意,我在该指定路径中有该文件。所以这个错误一定是由其他原因引起的。

【问题讨论】:

  • npm run build前添加RUN ls -la /app/src/environments/并附上结果

标签: angular docker


【解决方案1】:

所以我需要反映完全相同的数据结构。而不是

COPY src .

应该是的

COPY src ./src

感谢@jrichardsz 提示我一些调试步骤。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-10-13
    • 2020-09-15
    • 1970-01-01
    • 2019-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-05
    相关资源
    最近更新 更多