I want to use variables across multi-stage docker builds. Similar to This question (unanswered at the time of writing.)
My specific use case is to build my Go project in a builder
stage and save the directory this is done in in a variable and use the same variable in the next stage(s): The BUILD_DIR
variable.
My Dockerfile is (The example in the commented lines doesn't work.):
FROM golang:1.11.5 as builder
WORKDIR /project-name
# What I want to do:
#ENV BUILD_DIR /project-name
#WORKDIR ${BUILD_DIR}
# Vendored dependencies of my project:
COPY ./vendor ./vendor
COPY ./*.go ./
# Source code:
COPY ./go.* ./
RUN GOFLAGS=-mod=vendor GOOS=linux go build .
FROM something-else:some-version
WORKDIR some-folder
# Executable from previous stage:
COPY --from=builder /project-name/executable-name .
# Config files:
COPY ./conf ./conf
# What I want to do to copy the executable:
#COPY --from=builder /${BUILD_DIR}/executable-name .
ENTRYPOINT ["./executable-name"]