在 windows 容器上使用复杂的 powershell 脚本构建 Dockerfile

Dockerfile build using complex powershell script on windows container

我正在尝试使用运行 powershell 的 docker 文件构建一个 docker 图像。嵌入式 powershell 脚本已经在 docker 文件之外进行了测试,但是当 运行 docker 文件时它有错误。

来自 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

脚本的目的是将新内容添加到配置文件的特定行

知道语法有什么问题吗?

为了解决这个问题,我认为您需要在替换数据库名称的命令部分中添加额外的双引号。为了展示一个例子,我在下面粘贴了一个 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 看起来会有所不同,但是如果您采用我上面使用的示例并在字符串中的那些双引号周围添加额外的双引号,那么您应该会发现它按预期工作。

有任何问题,请告诉我。