use*_*531 3 shell powershell command-prompt docker line-continuation
我在 Windows PC 上关注https://docs.docker.com/get-started/06_bind_mounts/#start-a-dev-mode-container并卡在这里:
\n\n\n运行以下命令。我们\xe2\x80\x99稍后会解释\xe2\x80\x99发生了什么:
\n
docker run -dp 3000:3000 \\\n -w /app -v "$(pwd):/app" \\\n node:12-alpine \\\n sh -c "yarn install && yarn run dev"\n
Run Code Online (Sandbox Code Playgroud)\n\n\n如果您使用的是 PowerShell,请使用以下命令:
\n
docker run -dp 3000:3000 `\n -w /app -v "$(pwd):/app" `\n node:12-alpine `\n sh -c "yarn install && yarn run dev"\n
Run Code Online (Sandbox Code Playgroud)\n使用命令提示符时,我收到错误(尝试了多种变体,如下所示),而使用 PowerShell 时,我似乎没有收到错误,但没有运行任何内容,如执行时所示docker ps
。\n请注意,我宁愿使用命令提示符而不是 PowerShell,因为我可以在 PC 上通过 ComandPrompt 使用 Linux 命令。
在命令提示符下使用 Docker(以及与 PowerShell 相关的刻度线)时,反斜杠有何意义?
\n我发现它的docker run -dp 3000:3000 -w /app -v "%cd%:/app" node:12-alpine sh -c "yarn install && yarn run dev"
工作没有错误(去掉反斜杠,放在一行,并使用%cd%
而不是$(pwd)
),但仍然想知道为什么在示例中使用确切的脚本会导致错误。
使用命令提示符
\nC:\\Users\\michael\\Documents\\Docker\\app>docker run -dp 3000:3000 \\\ndocker: invalid reference format.\nSee \'docker run --help\'.\n\nC:\\Users\\michael\\Documents\\Docker\\app> -w /app -v "$(pwd):/app" \\\n\'-w\' is not recognized as an internal or external command,\noperable program or batch file.\n\nC:\\Users\\michael\\Documents\\Docker\\app> node:12-alpine \\\nThe filename, directory name, or volume label syntax is incorrect.\n\nC:\\Users\\michael\\Documents\\Docker\\app> sh -c "yarn install && yarn run dev"\nsh: yarn: command not found\n\nC:\\Users\\michael\\Documents\\Docker\\app>docker run -dp 3000:3000 \\ -w /app -v "$(pwd):/app" \\ node:12-alpine \\ sh -c "yarn install && yarn run dev"\ndocker: invalid reference format.\nSee \'docker run --help\'.\n\nC:\\Users\\michael\\Documents\\Docker\\app>docker run -dp 3000:3000 -w /app -v "$(pwd):/app" node:12-alpine sh -c "yarn install && yarn run dev"\ndocker: Error response from daemon: create $(pwd): "$(pwd)" includes invalid characters for a local volume name, only "[a-zA-Z0-9][a-zA-Z0-9_.-]" are allowed. If you intended to pass a host directory, use absolute path.\nSee \'docker run --help\'.\n\nC:\\Users\\michael\\Documents\\Docker\\app>\n
Run Code Online (Sandbox Code Playgroud)\n使用PowerShell
\nPS C:\\Users\\michael\\Documents\\Docker> docker run -dp 3000:3000 `\n>> -w /app -v "$(pwd):/app" `\n>> node:12-alpine `\n>> sh -c "yarn install && yarn run dev"\n849af42e78d4ab09242fdd6c3d03bcf1b6b58de984c4485a441a2e2c88603767\nPS C:\\Users\\michael\\Documents\\Docker> docker ps\nCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES\nPS C:\\Users\\michael\\Documents\\Docker>\n
Run Code Online (Sandbox Code Playgroud)\n
mkl*_*nt0 10
\n\n仍然想知道为什么在示例中使用确切的脚本会导致错误。
\n
因为带有行结束字符的命令\\
适用于POSIX 兼容的 shell,例如bash
,而不是cmd.exe
POSIX 兼容 shell ( sh
, bash
, dash
, ksh
, zsh
):
cmd.exe
:
^
一般用于续行和转义(仅在不带引号的参数中)。%varName%
引用环境变量(唯一支持的变量类型)。"..."
字符串(插值)。电源外壳:
\n\n潜在的行延续陷阱:在讨论的所有 shell 中,转义字符必须是行中的最后一个字符- 甚至不允许尾随(行内)空白(因为转义字符将应用于它而不是换行符)。
\n上述信息总结如下表:
\n特征 | POSIX shell\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0 \xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0_ | cmd.exe\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\ xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0_ | PowerShell\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\ xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0_ |
---|---|---|---|
续行符/转义符 | 反斜杠 ( \\ ) | 插入符 ( ^ ) | 反引号 ( ` ) |
双引号字符串(插值) | \xe2\x9c\x85 | \xe2\x9c\x85 | \xe2\x9c\x85 |
单引号字符串(逐字) | \xe2\x9c\x85 | \xe2\x9d\x8c | \xe2\x9c\x85 |
获取/设置环境变量 | $varName /export varName=... | %varName% /set varName=... | $env:varName /$env:varName = ... |
获取/设置仅 shell 变量 | $varName /varName=... | \xe2\x9d\x8c (不存在此类变量,但您可以使用 限制 env.vars. 的范围setlocal ) | $varName /$varName = ... |
命令替换、子表达式 | $(...) | \xe2\x9d\x8c | (...) / $(...) ,特别是。在字符串中 |
请注意根据符号两侧的空格重新设置变量:=
=
。cmd.exe
,此类空格很重要,并且成为变量/值名称的一部分,因此通常要避免。$var = \'hi!\'
)也可以看看:
\nhttps://hyperpolyglot.org/shell可以更全面地并置这些 shell,但请注意,截至撰写本文时,有关 PowerShell 的信息还不完整。
\nSage Pourpre 的有用答案,提供了各个 shell 的行延续文档的链接。
\n 归档时间: |
|
查看次数: |
2173 次 |
最近记录: |