由于使用 PowerShell 执行 Dockerfile 构建步骤,使用 Remote-Container 打开文件夹失败

Opening folder with Remote-Container fails due to PowerShell being used to execute Dockerfile build steps

我希望使用远程容器功能测试新的远程开发扩展。我抓取了 sample Python 项目并使用 Remote-Containers: Open Folder in Container... 函数打开它。

初始化过程开始正常,逐步完成一些 Docker 构建,没有问题。 Docker 文件中的第 1-3 步成功,然后第 4 步(Docker 文件的第 13/14 行)抛出异常并退出,因为 RUN 命令包含 AND_IF 运算符 (&&)。这是因为它作为子命令传递给不支持 &&.

的 PowerShell

我已经按照 instructions 准备我的系统以使用 Remote-Containers 功能,包括将我的驱动器(C: 和 D:)添加到共享驱动器。

到目前为止我尝试过的故障排除:

None 以上步骤产生了不同的结果。

Docker 检查显示 Config->Shell 设置为:

"Shell":
  [
    "powershell",
    "-Command",
    "$ErrorActionPreference = 'Stop'; $ProgressPreference = 'SilentlyContinue';"
  ]

Config->Cmd 设置为:

"Cmd":
  [
    "powershell",
    "-Command",
    "$ErrorActionPreference = 'Stop'; $ProgressPreference = 'SilentlyContinue';",
    "apt-get update     && apt-get -y install --no-install-recommends apt-utils 2>&1"
  ]

完整的容器配置是 here

异常的原因很明显,但我无法辨别为什么 Dockerfile RUN 指令会像上面那样传递给 PowerShell。

我是 运行 Visual Studio 代码 - Insiders (1.36.0-insider) 和 Docker Engine 18.09.2 on Windows 10 (1809)。

当第 4 步失败时,异常会产生以下错误(我已经包含了前面的成功步骤作为上下文;为简洁起见,在第 2 步中将 pip 安装展平):

Setting up container for folder: d:\Development\vscode-remote-try-python-master
Run: docker build -f d:\Development\vscode-remote-try-python-master\.devcontainer\Dockerfile -t vsc-vscode-remote-try-python-master-486294f4d73f25a657ec08f53ff07d5f d:\Development\vscode-remote-try-python-master
Sending build context to Docker daemon  24.06kB
Step 1/13 : FROM python:3
 ---> 22a423a5db36
Step 2/13 : RUN pip install pylint
 ---> Running in 23380af29dd1
Successfully installed astroid-2.2.5 colorama-0.4.1 isort-4.3.20 lazy-object-proxy-1.4.1 mccabe-0.6.1 pylint-2.3.1 six-1.12.0 typed-ast-1.4.0 wrapt-1.11.1
Removing intermediate container 23380af29dd1
 ---> 5569fa48c9c5
Step 3/13 : ENV DEBIAN_FRONTEND=noninteractive
 ---> Running in 941086f674cb
Removing intermediate container 941086f674cb
 ---> b8b2fd47bdb1
Step 4/13 : RUN apt-get update     && apt-get -y install --no-install-recommends apt-utils 2>&1
 ---> Running in defcc073adcf
At line:1 char:91
+ ... ; $ProgressPreference = 'SilentlyContinue'; apt-get update && apt-get ...
+                                                                ~~
The token '&&' is not a valid statement separator in this version.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordEx 
   ception
    + FullyQualifiedErrorId : InvalidEndOfLine

The command 'powershell -Command $ErrorActionPreference = 'Stop'; $ProgressPreference = 'SilentlyContinue'; apt-get update     && apt-get -y install --no-install-recommends apt-utils 2>&1' returned a non-zero code: 1
Failed: Building an image from the Dockerfile.
Command failed: C:\Program Files\Docker\Docker\Resources\bin\docker.exe build -f d:\Development\vscode-remote-try-python-master\.devcontainer\Dockerfile -t vsc-vscode-remote-try-python-master-486294f4d73f25a657ec08f53ff07d5f d:\Development\vscode-remote-try-python-master

这是其他人的普遍经历吗?将不胜感激任何解决方案或进一步故障排除的建议。

这个问题似乎是由 Windows 容器的使用引起的,正如本 GitHub issue 中所讨论的那样。

Yeah, unfortunately Windows Containers are not supported and using LCOW in "Windows Containers mode" for Docker Desktop is not something we really support right now given its experimental state.

LCOW still has gaps like supporting single file bind mounting that can cause issues and things like PostgreSQL are called out as not working yet. See here and here.

The Windows recommendation right now is primarily to use LCOW on an exception basis:

When to use Moby VM

Right now, we recommend the Moby VM method of running Linux containers to people who:

  • Want a stable container environment. This is the Docker for Windows default.
  • Run Windows or Linux containers, but rarely both at the same time.
  • Have complicated or custom networking requirements between Linux containers.
  • Don't need kernel isolation (Hyper-V isolation) between Linux containers.

When to use LCOW

Right now, we recommend LCOW to people who:

  • Want to test our newest technology.
  • Run Windows and Linux containers at the same time.
  • Need kernel isolation (Hyper-V isolation) between Linux containers.