Search code examples
powershelldockerdockerfiledocker-for-windowswindows-container

Dockerfile build using complex powershell script on windows container


I am trying to build a docker image with a dockerfile that runs powershell. The embedded powershell script has already been tested outside of the dockerfile, but when running the dockerfile it has errors.

From docker file:

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 \
"

I got the following error:

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

The purpose of the script is to add new content to specific line on the config file

any idea what wrong with the syntax?


Solution

  • In order to resolve this issue I think you need to add additional double quotes into the section of your command which replaces the database name. To show an example I have pasted a Dockerfile below which produced the results I expected based on what I understand you are trying to achieve in the question.

    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; \
    "
    

    Obviously I expect your Dockerfile will look different, but if you take the example I have used above and add the additional double quotes around those that you have inside your string then you should find that it works as expected.

    Any questions, let me know.