Powershell停止解析和docker

ubof19bj  于 2023-05-16  发布在  Docker
关注(0)|答案(1)|浏览(103)

我有一个为Docker提供支持的Powershell脚本。新的PowerShell核心版本(7.3)有一个突破性的变化:
https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_parsing?view=powershell-7.3#passing-arguments-that-contain-quote-characters
我相当肯定这与发生的事情有关,但是在docker的上下文中,我不知道如何使用它?
以下是我的脚本:

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

无论我把Stop Parsing标记(--%)放在哪里,都会得到错误:

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

k5ifujac1#

TLDR在PS脚本顶部附近添加以下内容:

$PSNativeCommandArgumentPassing = 'legacy'

接下来的方法是重构以使用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

相关问题