Search code examples
dockerpowershell-core

Powershell Stop Parsing and docker


I have a Powershell script that powers Docker. With the new core version of Powershell (7.3) there is a breaking change:

https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_parsing?view=powershell-7.3#passing-arguments-that-contain-quote-characters

Im fairly certain this is related to whats happening, but in the context of docker I cant figure out how to use it?

Here is my script:

param (
    [string]$target = "build"
)    

Write-Host "Running npm run $target"
$path = (Get-Item -Path ".\").FullName

docker run -it --memory="500m" --memory-swap="1g" --rm `
            --mount type=bind,src="$path\src/MyProject/wwwroot/dist",dst=/work/wwwroot/dist `
            --mount type=bind,src="$path\src/MyProject/fed/dist",dst=/work/fed/dist `
            --mount type=bind,src="$path\src/MyProject/fed/.storybook",dst=/work/fed/.storybook `
            --mount type=bind,src="$path\src/MyProject/fed/src",dst=/work/fed/src `
            mytag npm run $target

No matter where I put the Stop Parsing token (--%) I get the error:

ParserError: C:\Projects\MyProject\rundocker.ps1:12
Line |
  12 |                      --mount type=bind,src="$path\src/MyPRoject/wwwroot/dis …
     |                        ~
     | Missing expression after unary operator '--'.

Solution

  • TLDR Add the following near the top of your PS script:

    $PSNativeCommandArgumentPassing = 'legacy'
    

    The way going forward would be to refactor to use Start-Process:

    Start-Process -FilePath "docker" -ArgumentList "run","-it","--memory=500m","--rm","--mount type=bind,src=$path,dst=/work/wwwroot/dist","busybox"
    
    > ./test.ps1
    Running npm run build
    PS /home/user/source/so58> Unable to find image 'busybox:latest' locally
    latest: Pulling from library/busybox
    a58ecd4f0c86: Pull complete 
    Digest: sha256:62d10dc54dc47079c6e1e21f1642dc5a3633f1b1658940c5b8554a599b006e53
    Status: Downloaded newer image for busybox:latest