I am trying to build a multistage dockerfile with following stage but all stages are getting executed.
I always c that git clone is getting executed even if I have specified EXECUTION_ENV=local
in the docker build
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 tried:
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.
Note:
If I enable "features": { "buildkit": true}
in the docker daemon and run the cmd docker build -t somecontainer --build-arg GIT_TOKEN=abc:123 --build-arg EXECUTION_ENV=local.
I get the following error
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
Skipping stages only works with BuildKit. See the discussion here and article here.
As for the error you're getting, you should be getting it with or without BuildKit as you cannot use build arguments in COPY
instruction. The difference being that with BuildKit Docker will refuse to even start the build and without it the build will fail on COPY
instruction.
What you need to do is to create an additional alias for the image you want to copy from using the fact that FROM
instruction resolves the build args:
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"]