【问题标题】:Dockerfile build using complex powershell script on windows container在 Windows 容器上使用复杂的 powershell 脚本构建 Dockerfile
【发布时间】:2017-04-02 12:20:27
【问题描述】:

我正在尝试使用运行 powershell 的 dockerfile 构建 docker 映像。嵌入的 powershell 脚本已经在 dockerfile 之外进行了测试,但是在运行 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

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

知道语法有什么问题吗?

【问题讨论】:

  • 我不知道 powershell,但看起来你的双引号字符串 inside 有双引号(特别是 ...replace('database = "telegraf"'...),这通常不起作用.
  • 试过没有.. 相同的结果,它与我相信的 docker 文件语法有关
  • 因为这种疯狂,我不再使用 dockerfile powershell 条目了。我将我所有的内容都放在 PS1 文件中,COPY 将它放入容器中并在其中执行。任何类型的 DSC 脚本等也是如此。请记住,dockerfile 中的每个 Run 命令都会为您的图像添加另一层,而带有一堆语句的单个 PS1 文件仍然是单层

标签: powershell docker dockerfile docker-for-windows windows-container


【解决方案1】:

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

有任何问题,请告诉我。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-04-12
    • 2019-08-20
    • 2021-08-02
    • 2011-10-20
    • 2020-02-18
    • 2019-09-10
    • 2017-07-02
    • 1970-01-01
    相关资源
    最近更新 更多