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

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

我正在尝试使用运行 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

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

知道语法有什么问题吗?

最佳答案

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

有任何问题,请告诉我。

关于powershell - 在 Windows 容器上使用复杂的 powershell 脚本构建 Dockerfile,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43168227/

相关文章:

powershell - 批处理脚本可以知道它是否是从 PowerShell 调用的吗?

docker - docker-本地DNS

java - 如何在基于java的docker镜像中安装包管理器

ruby-on-rails - 用Dokku开始delayed_job

docker - 如何从另一个容器连接到独立的 selenium-firefox 容器

powershell - CMD/MS-DOS 中的 ASCII 艺术

node.js - Node 'http-server' 只能从 powershell 以管理员身份工作

powershell - 使用 Gmail API 在 Powershell 中使用 Invoke-WebRequest 发送电子邮件

shell - 为什么 && 而不是新的 RUN

docker - Openshift在创建应用程序时传递Dockerfile或Docker Image Environment变量