microsoft / iis-docker

Dockerfile for IIS
https://hub.docker.com/r/microsoft/iis/
MIT License
288 stars 128 forks source link

Docker run -e (enviromental variables) not working. #207

Open Edgaras91 opened 4 months ago

Edgaras91 commented 4 months ago

I have below dockerfile/docker image. When I run docker run with environmental variables, they do end up in the container (checked using PowerShell command Get-Childitem env:) but the IIS app in inetpub/wwroot is not picking these up (I am using Microsoft.Configuration.ConfigurationBuilders.EnvironmentConfigBuilder to read these, tested locally using IIS express which works.)

I did give a read on this issue: https://github.com/microsoft/iis-docker/issues/75 but I still don't understand why it doesn't work. Could not find any official documentation/examples on environmental variables for framework images.

Could someone explain how to get environmental variables into IIS site using dotnet framework image? My experience comes from dot net core, and I expected it to work similarly.

FROM mcr.microsoft.com/dotnet/framework/sdk:4.8 AS build

EXPOSE 80
EXPOSE 443

WORKDIR /app

# copy csproj and restore as distinct layers
COPY *.sln .
COPY ["_Website Application/*.vbproj", "./_Website Application/"]
COPY ["_Website Application/*.config", "./_Website Application/"]
COPY NuGet.config .
RUN nuget restore -ConfigFile NuGet.config

# copy everything else and build app
COPY ["_Website Application/.", "./_Website Application/"]
COPY ["ExternalLibraries/.", "./ExternalLibraries/"]
WORKDIR "/app/_Website Application"
RUN msbuild /p:Configuration=Release -r:False

FROM mcr.microsoft.com/dotnet/framework/aspnet:4.8 AS runtime

# Install IIS Rewrite Module
WORKDIR /install
ADD https://download.microsoft.com/download/1/2/8/128E2E22-C1B9-44A4-BE2A-5859ED1D4592/rewrite_amd64_en-US.msi rewrite_amd64_en-US.msi
RUN Write-Host 'Installing URL Rewrite' ; Start-Process msiexec.exe -ArgumentList '/i', 'rewrite_amd64_en-US.msi', '/quiet', '/norestart' -NoNewWindow -Wait;

#Add HTTPS 443 SSL Binding to localhost host name.
COPY docker-enable-localhost-ssl.ps1 .
RUN powershell.exe ./docker-enable-localhost-ssl.ps1

WORKDIR /inetpub/wwwroot
COPY --from=build ["/app/_Website Application/.", "./"]
erenes commented 4 months ago

The issue is that IIS is running a different user than the docker user that starts the entrypoint. The docker-iis image uses a helper process as entrypoint (IIS.ServiceMonitor) that reads the environment variables and writes them to IIS config using appcmd, e.g.: C:\Windows\system32\inetsrv\appcmd.exe set config -section:system.applicationHost/applicationPools /+"[name='DefaultAppPool'].environmentVariables.[name='MyVariable',value='MyVariableValue']" /commit:apphost

The asp.net image also uses this same entry point, so I would expect that it would work the same way.

Perhaps you can verify that the updated configuration is indeed written to the applicationHost.config file on startup? Typically it's in system32\inetsrv\config