如何在同一 Dockerfile 中为 Windows 和 Linux 容器实施条件包安装

How to implement a conditional package install for Windows and Linux containers in the same Dockerfile

我想对 Windows 和 Linux 容器使用相同的 Dockerfile。它使用构建参数顶部定义基本图像,如:

FROM $SDK_REPO:$SDK_VERSION AS build-env

它还定义了一个名为 $PLATFORM 的参数,它是 linuxwindows.

现在我只想在为 linux 构建映像时安装包。 我尝试使用 if 开关,例如:

RUN if [ $PLATFORM = linux ]; then apt-get update && apt-get install -y <mypackage>; fi

显然,这不适用于 windows,因为它使用 bash 语法。

是否可以对 windows 和 linux 容器使用相同的 dockerfile,并且仅当我的平台参数为 linux 时才进行 apt-get 安装?

经过一些调查,我找到了一种方法,可以按照以下线程的示例进行操作:Single script to run in both Windows batch and Linux Bash?

在我的例子中,我使用:

RUN :; apt-get update && apt-get install -y

这只会在 Linux 上执行,但会在 Windows 上跳过。