2

我有一个已经 dockerized 的 Blazor webassembly 项目,我正在尝试在启动时读取容器中设置的环境变量。如果我执行以下操作,我可以在容器中运行 shell 并回显变量。

docker run -e DEBUG=false --name blazortest dockerizedclientsideblazor

但是,如果我尝试使用此命令读取应用程序中的环境变量。

Environment.GetEnvironmentVariable("DEBUG")

它返回为 null 并且尝试读取已安装的文件也是如此。可以向我解释为什么我会遇到这种情况吗?

我的 Dockerfile 看起来像这样。

FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS build-env
WORKDIR /app
COPY . ./
RUN ls -la ./
RUN dotnet publish -c Release -o output

FROM nginx:alpine
WORKDIR /var/www/web
COPY --from=build-env /app/output/wwwroot .
COPY nginx.conf /etc/nginx/nginx.conf
EXPOSE 80 443
4

3 回答 3

1

Coming back to this question with my own answer.

Trying to retrieve the value of environment variables on the server(in this case a container) won't work since the code is being being downloaded and run on the client.

So the environment variables set on the server will not be present on the client side.

The solution would be to use appsettings instead.

于 2021-04-19T20:57:17.670 回答
1

事实上,即使在客户端执行,您也可以指定环境变量。

多种选择:

于 2021-07-29T23:25:41.607 回答
0

它返回为 null 并且尝试读取已安装的文件也是如此。

是的。您的 WebAssembly 应用程序不在该容器中运行,它在最终用户的浏览器中运行。

您可以从运行在 nginx 上的服务中读取变量和那些文件。

于 2020-07-08T01:24:41.770 回答