无法在docker多阶段构建中使用ARG

时间:2020-04-16 19:18:41

标签: docker dockerfile docker-multi-stage-build

我正在尝试在docker build过程的多阶段中使用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而失败,因为它无法识别获取图像的语法。

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

1 个答案:

答案 0 :(得分: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/