powershell - Powershell中的Docker CP参数扩展

标签 powershell docker docker-for-windows docker-cp

我想运行这样的事情:

$docker_container_name = "iar_build_container"
...
$cp_arguments =  $docker_container_name + ":C:/docker_work/IAR/Debug " + $docker_artifacts + "/Debug"

"Copying out the build artifacts: $cp_arguments"
docker cp "$cp_arguments"

输出为:
Copying out the build artifacts: iar_build_container:C:/docker_work/IAR/Debug ./docker_artifacts/Debug
"docker cp" requires exactly 2 arguments.
See 'docker cp --help'.

Usage:  docker cp [OPTIONS] CONTAINER:SRC_PATH DEST_PATH|-
        docker cp [OPTIONS] SRC_PATH|- CONTAINER:DEST_PATH

Copy files/folders between a container and the local filesystem

如果我对docker cp命令进行硬编码,则可以使用,如果使用以下命令,则可以使用:
docker cp iar_build_container:C:/docker_work/IAR/Debug $docker_artifacts/Debug

所以我在扩展第一个参数即容器名称和冒号时遇到问题。

编辑:
这可行,但是感觉很hacky:
$cp_arguments = "cp " + $docker_container_name + ":C:/docker_work/IAR/Debug " + `
                        $docker_artifacts + "/Debug"

"Copying out the build artifacts: $cp_arguments"
Start-Process -FilePath "docker" -ArgumentList "$cp_arguments" -Wait

最佳答案

扩展@nishaygoyal答案。在PowerShell中运行可执行文件(例如docker)与在CMD提示符下运行可执行文件不同。在PowerShell中,参数以字符串数组形式传递,而不是像CMD提示符一样以空格分隔的一系列字符串形式传递。因此,通过传递以空格分隔的参数字符串,PowerShell将其解释为单个参数和单个字符串。

因此,只需将参数更改为字符串数组,然后将“cp”移动为其中的一项即可。事情会起作用:

$docker_container_name = "iar_build_container"
$docker_artifacts = "./docker_artifacts"

$cp_arguments = @("cp",                                               `
                  "$($docker_container_name):C:/docker_work/IAR/Debug",  `
                  "$docker_artifacts/Debug")

docker $cp_arguments

编辑:

正如@Nick指出的那样,我们必须在字符串中使用子表达式运算符:$($docker_container_name)进行适当的字符串扩展,因为PowerShell会将$docker_container_name:C:解释为变量而不是$docker_container_name。对于PowerShell,冒号表示变量的范围,例如$global:foo。因此,我们需要使用子表达式运算符$()来正确定义用于字符串扩展的变量。

为什么像这样使用Start-Process起作用?
$cp_arguments = "cp " + $docker_container_name + ":C:/docker_work/IAR/Debug " + `
                        $docker_artifacts + "/Debug"

Start-Process -FilePath "docker" -ArgumentList "$cp_arguments" -Wait

好吧,根据Start-Process的特殊之处在于,-ArgumentList可以接受空格分隔的参数列表,并以CMD提示样式方式对待它们。

我们还可以使用EchoArgs来查看正作为参数传递的内容:
$docker_container_name = "iar_build_container"
$docker_artifacts = "./docker_artifacts"

#Original:

$cp_arguments =  $docker_container_name + ":C:/docker_work/IAR/Debug " + $docker_artifacts + "/Debug"

PS C:\> EchoArgs.exe docker cp "$cp_arguments"
Arg 0 is <docker>
Arg 1 is <cp>
Arg 2 is <iar_build_container:C:/docker_work/IAR/Debug ./docker_artifacts/Debug>

Command line:
"C:\ProgramData\chocolatey\lib\echoargs\tools\EchoArgs.exe" docker cp "iar_build_container:C:/docker_work/IAR/Debug ./docker_artifacts/Debug"

(请注意,我们传递了2个参数cp和字符串的其余部分。与传递数组相比:
$cp_arguments = @("cp",                                               `
                  "$($docker_container_name):C:/docker_work/IAR/Debug",  `
                  "$docker_artifacts/Debug")

PS C:\> EchoArgs.exe docker $cp_arguments
Arg 0 is <docker>
Arg 1 is <cp>
Arg 2 is <iar_build_container:C:/docker_work/IAR/Debug>
Arg 3 is <./docker_artifacts/Debug>

Command line:
"C:\ProgramData\chocolatey\lib\echoargs\tools\EchoArgs.exe" docker cp iar_build_container:C:/docker_work/IAR/Debug ./docker_artifacts/Debug

在这种情况下,您可以看到它“正确”地分割了参数

关于powershell - Powershell中的Docker CP参数扩展,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62053469/

相关文章:

powershell - 签署 PowerShell 脚本

Windows PowerShell : How to do standard-in redirection & passing parameters to commands properly?

docker - 为什么 Windows docker 容器在 sleep 命令完成之前提前退出?

Docker Gelf 驱动程序自定义字段

wordpress - 在Windows上使用Docker时的Wordpress写访问

Docker for windows sql express 卷数据不持久

powershell - 使用 powershell : Unwanted results 从 Outlook 获取今天的约会

.net - 从托管 powershell 中的脚本或命令检索参数

docker - Cassandra 容器数据库 NoNodeAvailableException

wif - 在Docker中启用Windows Identity Foundation