Windows Docker 添加自定义标头



我正在尝试使用Add-WebConfigurationPropertyX-Forwarded-For请求标头添加到mcr.microsoft.com/dotnet/framework/aspnet:4.8-windowsservercore-ltsc2019Docker 基础映像中。

我的 Dockerfile 看起来像:

FROM mcr.microsoft.com/dotnet/framework/aspnet:4.8-windowsservercore-ltsc2019
ARG source
RUN Add-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST' -filter "system.applicationHost/sites/site[@name='Default Web Site']/logFile/customFields" -name "." -value "@{logFieldName='X-Forwarded-For';sourceName='X-FORWARDED-FOR';sourceType='RequestHeader'}"
WORKDIR /inetpub/wwwroot
COPY ${source:-obj/Docker/publish} .

当我运行 docker 构建时,出现以下错误

Sending build context to Docker daemon  4.096kB
Step 1/5 : FROM mcr.microsoft.com/dotnet/framework/aspnet:4.8-windowsservercore-ltsc2019
---> 30379ad7580b
Step 2/5 : ARG source
---> Using cache
---> 90c760c37d26
Step 3/5 : RUN Add-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST' -filter "system.applicationHost/sites/site[@name='Default Web Site']/logFile/customFields" -name "." -value @{logFieldName='X-Forwarded-For';sourceName='X-FORWARDED-FOR';sourceType='RequestHeader'}
---> Running in 4210cd6ae606
Add-WebConfigurationProperty : Expected an operator name (eg. 'and')
Input: get-config("MACHINE/WEBROOT/APPHOST")/system.applicationHost/sites/site[
@name=Default Web Site]/logFile/customFields
Position: 86
Length: 3
Fragment: Web
At line:1 char:76
+ ... yContinue'; Add-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APP ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : NotSpecified: (:) [Add-WebConfigurationProperty]
, ArgumentException
+ FullyQualifiedErrorId : System.ArgumentException,Microsoft.IIs.PowerShel
l.Provider.AddConfigurationPropertyCommand
The command 'powershell -Command $ErrorActionPreference = 'Stop'; $ProgressPreference = 'SilentlyContinue'; Add-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST' -filter "system.applicationHost/sites/site[@name='Default Web Site']/logFile/customFields" -name "." -value @{logFieldName='X-Forwarded-For';sourceName='X-FORWARDED-FOR';sourceType='RequestHeader'}' returned a non-zero code: 1

Dockerfile 在没有RUN Add-WebConfigurationProperty的情况下构建良好。如果我在没有RUN Add-WebConfigurationProperty的情况下构建它,我可以运行容器并执行Add-WebConfigurationProperty命令,并看到它确实将字段添加到日志文件中。知道为什么它不能通过 Dockerfile 工作。

我也遇到了一些Windows Dockerfiles的转义问题,有时我会为我需要的东西拉一个powershell脚本。

在这种情况下,由于我假设您只想添加该字段,因此您可以使用:

-filter "system.applicationHost/sites/siteDefaults/logFile/customFields"

希望这有帮助!

谢谢!

最新更新