Blazor WebAssembly can't read environment variables or find files when running in Docker

Viewed 1107

I have a Blazor webassembly project which I have dockerized and I'm trying to read environment variables set in the container on startup. If I do the following I am able to run a shell in the container and echo the variable.

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

However if I try to read the environment variable in the application using this command.

Environment.GetEnvironmentVariable("DEBUG")

It returns as null and it's the same for trying to read mounted files. Can explain to me why I'm experiencing this?

My Dockerfile looks like this.

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
3 Answers

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.

It returns as null and it's the same for trying to read mounted files.

Yes. Your WebAssembly app does not run in that container, it runs in the Browser of the end users.

You could read the variable and those files from a Service running on nginx.

Related