gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 18:31:16 26 4
gpt4 key购买 nike

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

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com