在Windows容器上使用复杂的PowerShell脚本构buildDockerfile

我正在尝试使用运行PowerShell的dockerfile来构build一个Docker镜像。 embedded式powershell脚本已经在dockerfile之外进行了testing,但是在运行dockerfile时却有错误。

从docker文件:

RUN powershell -NoProfile -Command " \ $lines = (Get-Content c:\telegraf\telegraf.conf).replace('http://localhost:8086', 'http://publichost.com:8086'); \ Set-Content c:\telegraf\telegraf.conf $lines; \ $lines = (Get-Content c:\telegraf\telegraf.conf).replace('database = "telegraf"', 'database = "qa"'); \ Set-Content c:\telegraf\telegraf.conf $lines \ " 

我得到了以下错误:

 At line:1 char:97 + ... egraf.conf; $pos = [array]::indexof($lines, $lines -match global_ ... + ~ You must provide a value expression following the '-match' operator. At line:1 char:97 + ... egraf.conf; $pos = [array]::indexof($lines, $lines -match global_ ... + ~ Missing ')' in method call. At line:1 char:98 + ... $pos = [array]::indexof($lines, $lines -match global_tags); $ ... + ~~~~~~~~~~~ Unexpected token 'global_tags' in expression or statement. At line:1 char:109 + ... $pos = [array]::indexof($lines, $lines -match global_tags); $l ... + ~ Unexpected token ')' in expression or statement. + CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordEx ception + FullyQualifiedErrorId : ExpectedValueExpression 

脚本的目的是在configuration文件的特定行添加新的内容

任何想法有什么错误的语法?

为了解决这个问题,我想你需要添加额外的双引号到你的命令部分replace数据库名称。 为了显示一个例子,我已经粘贴了一个Dockerfile,根据我所了解的你在这个问题中想要达到的结果,在下面生成了我期望的结果。

 FROM microsoft/windowsservercore COPY telegraf.conf C:\\telegraf\\telegraf.conf RUN powershell -NoProfile -Command " \ $lines = (Get-Content c:\telegraf\telegraf.conf).replace('http://localhost:8086', 'http://publichost.com:8086'); \ Set-Content c:\telegraf\telegraf.conf $lines; \ $lines = (Get-Content c:\telegraf\telegraf.conf).replace('database = """telegraf"""', 'database = """qa"""'); \ Set-Content c:\telegraf\telegraf.conf $lines; \ " 

显然,我期望你的Dockerfile看起来不一样,但是如果你拿上面我用过的例子,并且在你的string里面添加了双引号,那么你应该会发现它能够按照预期工作。

有任何问题,请告诉我。