【问题标题】:Windows Docker adding custom headersWindows Docker 添加自定义标头
【发布时间】:2020-01-14 00:32:46
【问题描述】:

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

我的 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 build 时出现以下错误

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

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

【问题讨论】:

    标签: windows docker dockerfile windows-container windows-server-container


    【解决方案1】:

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

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

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

    希望这会有所帮助!

    谢谢!

    【讨论】:

      猜你喜欢
      • 2012-05-17
      • 1970-01-01
      • 2013-11-13
      • 2016-10-18
      • 2013-05-15
      • 2012-04-08
      • 2012-01-21
      • 2020-08-24
      • 2019-01-18
      相关资源
      最近更新 更多