我想在多阶段 docker 构建中使用变量。类似于这个问题(在撰写本文时尚未回答。)
我的具体用例是在一个builder
阶段构建我的 Go 项目并将完成的目录保存在一个变量中,并在下一个阶段使用相同的变量:BUILD_DIR
变量。
我的 Dockerfile 是(注释行中的示例不起作用。):
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"]