Powershell、反斜杠和向Docker发送命令
这在一定程度上与PowerShell有关,但我有一个我正在努力解决的边缘案例,这可能是因为我缺乏PowerShell知识 我有一个PowerShell脚本,它需要在Powershell、反斜杠和向Docker发送命令,powershell,Powershell,这在一定程度上与PowerShell有关,但我有一个我正在努力解决的边缘案例,这可能是因为我缺乏PowerShell知识 我有一个PowerShell脚本,它需要在docker run命令中选择性地包含-v选项 以下是脚本: $pwd = (Get-Location) # If GEM_HOME is declared, see if it is in the current directory. If not, we need # to volume-mount it and change
docker run
命令中选择性地包含-v
选项
以下是脚本:
$pwd = (Get-Location)
# If GEM_HOME is declared, see if it is in the current directory. If not, we need
# to volume-mount it and change the variable value.
$gem_mount_cmd = ""
$gem_env_var = "-e GEM_HOME"
if (Test-Path env:GEM_HOME)
{
$gem_home = $env:GEM_HOME
$parent = (Split-Path -parent $gem_home)
if ((Join-Path $parent '') -ne $pwd)
{
$gem_mount_cmd = "-v ""`$('$parent' -replace '\\','/'):/gems"""
$gem_env_var = "-e GEM_HOME='/gems/"+(Split-Path -leaf $gem_home)+"'"
}
}
$cmd = "& docker run --rm -it $gem_env_var $gem_mount_cmd -v ${pwd}:/srv alpine bash"
Write-Output $cmd
& $cmd
目前,运行该脚本将导致PowerShell错误,即关于docker run
的错误不会被识别为cmdlet、函数、脚本文件或可操作程序的名称
我也试过:
& docker run --rm -it $gem_env_var $gem_mount_cmd -v ${pwd}:/srv alpine bash
这给了我一个Docker错误,抱怨“未知速记标志:'r'in-replace”
我还尝试将$gem\u mount\u cmd
的赋值替换为:
$gem_mount_cmd = "-v $($parent -replace '\\','/'):/gems"
但这让我回到了“来自守护进程的错误响应:无效模式”错误,链接问题的OP遇到了这个错误
我还阅读并修改了脚本:
$pwd = (Get-Location)
# If GEM_HOME is declared, see if it is in the current directory. If not, we need
# to volume-mount it and change the variable value.
$gem_mount_cmd = ""
$gem_env_var = "-e GEM_HOME"
if (Test-Path env:GEM_HOME)
{
$gem_home = $env:GEM_HOME
$parent = (Split-Path -parent $gem_home)
if ((Join-Path $parent '') -ne $pwd)
{
$gem_mount_cmd = "-v $($parent -replace '\\','/'):/gems"
$gem_env_var = "-e GEM_HOME='/gems/"+(Split-Path -leaf $gem_home)+"'"
}
}
$params = 'run', '--rm', '-it',
$gem_env_var, $gem_mount_cmd, '-v ${pwd}:/srv',
'alpine', 'bash'
& docker @params
但这给了我同样的Docker“无效模式”错误
我需要做什么才能让它工作?我认为一个选项是让脚本嵌入两个不同版本的docker run
命令,例如:
$pwd = (Get-Location)
# If GEM_HOME is declared, see if it is in the current directory. If not, we need
# to volume-mount it and change the variable value.
$gem_mount_cmd = ""
$gem_env_var = "-e GEM_HOME"
if (Test-Path env:GEM_HOME)
{
$gem_home = $env:GEM_HOME
$parent = (Split-Path -parent $gem_home)
if ((Join-Path $parent '') -ne $pwd)
{
$gem_mount_cmd = "-v $($parent -replace '\\','/'):/gems"
$gem_env_var = "-e GEM_HOME='/gems/"+(Split-Path -leaf $gem_home)+"'"
}
}
if ("$gem_mount_cmd" -ne "") {
& docker run --rm -it $gem_env_var -v "$($parent -replace '\\','/'):/gems" -v ${pwd}:/srv alpine bash
}
else {
& docker run --rm -it -v ${pwd}:/srv alpine bash
}
但感觉一定有更好的方法…我会动态构建参数数组,然后在命令上显示它:
$params = 'run', '--rm', '--it'
if (Test-Path env:GEM_HOME) {
$params += '-e', "GEM_HOME='/gems/$(Split-Path -Leaf $gem_home)'",
'-v', "$($parent -replace '\\', '/'):/gems"
}
$params += '-v', "${pwd}:/srv", 'alpine', 'bash'
& 'docker.exe' @params
尝试有条件地将值附加到
$params
数组<代码>$params='run'、'-rm'、'-it';if(测试路径env:GEM_HOME){$params+='-e',“GEM_HOME='/gems/$(拆分路径-Leaf$GEM_HOME)','-v',($parent-replace'\\','/')$参数+='-v',“${pwd}:/srv”,“alpine”,“bash”谢谢,@AnsgarWiechers。有一个小的输入错误-“$($parent-replace'\\','/'):/gems”,但现在可以了。