Dockerizing Angular应用:/app/src/environments/environment.prod



我正在尝试dockerize一个Angular应用程序。下面是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.jsonconfig:

{
"$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步抛出以下错误的原因是什么?我试着在互联网上寻找这个错误,并找到了一些资源(这个和这个),但这些对我没有任何帮助。

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..

这个错误似乎是不言自明的,但请注意,我在指定的路径中有那个文件。所以这个错误一定是由其他原因引起的。

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

COPY src .

必须是

COPY src ./src

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