3

我正在尝试构建具有以下阶段的多阶段 dockerfile,但所有阶段都在执行。

即使我EXECUTION_ENV=local在 docker build中指定了,我总是 c git clone 正在执行

Dockerfile:

ARG GIT_TOKEN=abc:1a2b3
ARG EXECUTION_ENV=local

# get dependencies from github
FROM alpine/git as gitclone-ci
WORKDIR /usr/src/
RUN git clone https://{GIT_USER_TOKEN}@github.com/something.git \
    && git clone https://{GIT_USER_TOKEN}@github.com/somethingelse.git

## in local dependencies are already available in the parent folder
FROM alpine/git as gitclone-local
WORKDIR /usr/src/
COPY ../something /usr/src/something 
COPY ../somethingelse /usr/src/somethingelse

FROM node:latest as builder
WORKDIR /usr/src
COPY --from=gitclone-${EXECUTION_ENV} /usr/src .
COPY package* ./
COPY src/ src/
RUN ["npm", "install"]

Docker build cmds 试过:

docker build -t somecontainer --build-arg GIT_TOKEN=abc:123 --build-arg EXECUTION_ENV=local.

docker build -t somecontainer --target builder --build-arg GIT_TOKEN=abc:123 --build-arg EXECUTION_ENV=local.

注意: 如果我"features": { "buildkit": true}在 docker daemon 中启用并运行 cmd docker build -t somecontainer --build-arg GIT_TOKEN=abc:123 --build-arg EXECUTION_ENV=local.,我会收到以下错误

failed to solve with frontend dockerfile.v0: failed to create LLB definition: failed to parse stage name "gitclone-$EXECUTION_ENV": invalid reference format: repository name must be lowercase
4

1 回答 1

5

跳过阶段仅适用于 BuildKit。请参阅此处的讨论和此处的文章。

至于您遇到的错误,您应该在有或没有 BuildKit 的情况下得到它,因为您不能在COPY指令中使用构建参数。不同之处在于,使用 BuildKit Docker 甚至会拒绝启动构建,而没有它,构建将在COPY指令上失败。

FROM您需要做的是使用指令解析构建参数的事实为要复制的图像创建一个额外的别名:

ARG GIT_TOKEN=abc:1a2b3
ARG EXECUTION_ENV=local

# get dependencies from github
FROM alpine/git as gitclone-ci
WORKDIR /usr/src/
RUN git clone https://{GIT_USER_TOKEN}@github.com/something.git \
    && git clone https://{GIT_USER_TOKEN}@github.com/somethingelse.git

## in local dependencies are already available in the parent folder
FROM alpine/git as gitclone-local
WORKDIR /usr/src/
COPY ../something /usr/src/something 
COPY ../somethingelse /usr/src/somethingelse

FROM gitclone-${EXECUTION_ENV} as intermediate

FROM node:latest as builder
WORKDIR /usr/src
COPY --from=intermediate /usr/src .
COPY package* ./
COPY src/ src/
RUN ["npm", "install"]
于 2020-08-12T14:29:59.873 回答