首页 > 解决方案 > Powershell错误:重定向运算符后缺少文件规范

问题描述

我刚开始Docker教程,下载后按照官方主页上的命令

PS C:\COCcal> cat > Dockerfile <<EOF
>> FROM busybox
>> CMD echo "Hello world! This is my first Docker image."
>> EOF

给我

At line:1 char:19
+ cat > Dockerfile <<EOF
+                   ~
Missing file specification after redirection operator.
At line:1 char:18
+ cat > Dockerfile <<EOF
+                  ~
The '<' operator is reserved for future use.
At line:1 char:19
+ cat > Dockerfile <<EOF
+                   ~
The '<' operator is reserved for future use.
At line:2 char:1
+ FROM busybox
+ ~~~~
The 'from' keyword is not supported in this version of the language.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : MissingFileSpecification

这个错误。我在谷歌上搜索了错误消息,但找不到与我相同的案例。是否缺少任何设置?或者我该怎么做才能使用这些命令?我提前感谢它。

标签: powershelldocker

解决方案


您似乎正在 PowerShell 中运行 Linux shell 命令。根据您的配置,这可能会有问题。我建议打开 Linux shell 或使用 PowerShell 支持的命令。

# Run the Set-Content Command Below
Set-Content -Path Dockerfile

# After Running the above command, I am prompted to enter data:
Value[0]: FROM busybox
Value[1]: CMD echo "Hello world! This is my first Docker image."
Value[2]:

运行上述命令时,系统会提示您输入Value[0]Value[1]等(前提是您在每行输入数据后按 Enter 键),直到您在没有任何其他输入的行上按 Enter 键。这些输入中的每一个都将位于Dockerfile.

关于PowerShell 中的重定向>运算符,仅支持、>>>&1。如果cat完全有效,那是因为它是一个别名。您可以运行Get-Alias cat以查看它映射到哪个命令。在我的系统上,即Get-Content.


如果您不需要具有 Linux 命令提供的相同体验,还有其他 PowerShell 方法可以完成此任务。下面只是一个例子。

$Content = @'
line 1 stuff
line 2 stuff
line 3 stuff
'@
$Content | Set-Content -Path Dockerfile

UTF8NoBOM有关使用编码的输出,请参见下文。

$Content = @'
line 1 stuff
line 2 stuff
line 3 stuff
'@
[IO.File]::WriteAllLines('C:\COCcal\Dockerfile',$Content,[Text.UTF8Encoding]::new($false))

我不能说相对于 Docker 运行这个 Docker 配置。因此,在尝试运行本机 PowerShell 命令时可能需要考虑一些事项。

  1. 如果Dockerfile需要存在某些行尾字符,则Set-Content可能会在 Docker 不喜欢的每一行上使用回车符和换行符的某种组合。
  2. Docker 可能期望Dockerfile. 虽然Set-Content确实提供了-Encoding参数,但它仍然可能无法提供您需要的东西。例如,您可以使用-Encoding UTF8. 在 Windows PowerShell 中,这将是带有 BOM 的 UTF8(在 Windows PowerShell 中,此命令没有 No BOM 选项)。Windows PowerShell 中的默认编码Default可能是ANSI. 在 PowerShell Core 中,默认编码是UTF8NoBOM.

推荐阅读