0

我正在尝试在多阶段的 docker 构建过程中使用 ARGS。

我的 Dockerfile 看起来像:

ARG DOCKER_REGISTRY=example.com/docker
FROM $DOCKER_REGISTRY/openjdk8:latest AS installer 
ARG APP_VERSION=6.3.0.78 
ARG DOCKER_REGISTRY 
ARG REPO_TYPE=snapshot 
ARG DB_VERSION=12.2.0.11-ee
ARG DB_TYPE=oracle ARG PASSWORD

ARG DOCKER_REGISTRY 
ARG DB_VERSION 
ARG DB_TYPE
FROM ${DOCKER_REGISTRY}/${DB_TYPE}/database:${DB_VERSION} 
ARG DB_VERSION 
ARG DB_TYPE
ARG PASSWORD
RUN mkdir -p /opt/oracle/script
COPY --from=installer /opt/installer/target_script/* /opt/oracle/scripts/

每当它在 docker 文件中获得第二个 FROM 步骤时,它就会失败并出现错误invalid reference format,因为它无法识别获取图像的语法。

我的假设是它不知道ARG在多阶段 docker 构建中进行处理。

4

1 回答 1

2

ARG 是有范围的。在第一个 FROM 行之前,它们是全局的并且仅在 FROM 行中可用。在每个阶段中,它们的范围一直到该阶段结束。所以你需要:

ARG DOCKER_REGISTRY=example.com/docker
# move the DB_VERSION and DB_TYPE above the first FROM line
ARG DB_VERSION 
ARG DB_TYPE
FROM $DOCKER_REGISTRY/openjdk8:latest AS installer 

# none of these args seem to do anything, unless you have
# ONBUILD steps that depend on them in your openjdk image
ARG APP_VERSION=6.3.0.78 
ARG DOCKER_REGISTRY 
ARG REPO_TYPE=snapshot 
ARG DB_VERSION=12.2.0.11-ee
ARG DB_TYPE=oracle ARG PASSWORD

FROM ${DOCKER_REGISTRY}/${DB_TYPE}/database:${DB_VERSION} 

# none of these args are used either
ARG DB_VERSION 
ARG DB_TYPE
ARG PASSWORD

RUN mkdir -p /opt/oracle/script
COPY --from=installer /opt/installer/target_script/* /opt/oracle/scripts/
于 2020-04-16T19:46:41.233 回答