Dockerfile(如果其他条件与FROM的外部arg一起使用)

时间:2019-02-21 09:54:54

标签: docker dockerfile

如何在外部ARG中使用FROM if else语句。例如在Dockerfile中:

    ARG MYARG

    if MYARG='true'{
    FROM mcr.microsoft.com/windows/servercore:1803
    }
    elseif MYARG='false'{
    FROM mcr.microsoft.com/windows/servercore:1809
    }
    .
    .
    .

1 个答案:

答案 0 :(得分:1)

Dockerfile中当前没有if / else语法。借助BuildKit,您可以利用在多阶段构建中仅构建依赖项的优势,以使用以下语法:

ARG BUILD_VERSION=1
FROM alpine AS base
RUN …

FROM base AS branch-version-1
RUN touch version1

FROM base AS branch-version-2
RUN touch version2

FROM branch-version-${BUILD_VERSION} AS after-condition

FROM after-condition 
RUN …

对此有更详细的描述:https://medium.com/@tonistiigi/advanced-multi-stage-build-patterns-6f741b852fae