3

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?

BozoJoe
  • 6,117
  • 4
  • 44
  • 66
cfircoo
  • 417
  • 2
  • 8
  • 23
  • I don't know powershell, but it looks like you have double quotes *inside* your double-quoted string (specifically at `...replace('database = "telegraf"'...`), which typically wouldn't work. – larsks Apr 02 '17 at 12:25
  • tried without.. same results, it related to docker file syntax I belive – cfircoo Apr 02 '17 at 12:28
  • 2
    I don't mess with dockerfile powershell entries anymore because of this crazyness. I put all my content in PS1 file, `COPY` it into container and execute inside it. The same goes for any sort of DSC scripts etc. Remember that also each `Run` command in dockerfile will add another layer to your image, while single PS1 file with bunch of statements is still a single layer – Gregory Suvalian Apr 02 '17 at 12:42

1 Answers1

4

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.

Chris Lawrence
  • 481
  • 1
  • 6
  • 14