Dockerized Angular应用程序看不到环境变量...无法读取未定义的属性

时间:2020-09-04 06:34:35

标签: angular docker environment-variables

我一直严格遵守本指南,以设置用于从外部文件读取环境变量的角度应用程序。

https://medium.com/@ychetankumarsarma/how-to-manage-angular-application-configurations-for-different-environments-with-docker-and-8ff8c55a1c7d

这是dockerfile:

map_fn

这是运行它的命令: FROM node:lts-alpine AS builder WORKDIR /app COPY . . RUN npm install RUN npm run build FROM nginx:alpine COPY --from=builder /app/dist/* /usr/share/nginx/html/ CMD ["/bin/sh", "-c", "envsubst < /usr/share/nginx/html/env.template.js > /usr/share/nginx/html/env.js && exec nginx -g 'daemon off;'"]

现在这3个环境文件按以下顺序:$ docker run -d --name fe --env-file envs -p 80:80 --network mynetwork registry.gitlab.com/my-project-name:latest

environment.ts, env.js, env.template.js
export const environment = {
  production: false,
  
  backendAddress: window["env"]["backendAddress"]
};
(function(window) {
    window["env"] = window["env"] || {};
  
    window["env"]["backendAddress"] = "backendAddress";
})(this);
  

进入容器并进行(function(window) { window["env"] = window["env"] || {}; window["env"]["backendAddress"] = "${BACKEND_API_URL}"; })(this); 时,我得到了正确的值。从浏览器访问应用程序时,由于存在错误,因此页面不会加载:

echo $BACKEND_API_URL

0 个答案:

没有答案