Mickey Hovel
Mickey Hovel

Reputation: 1070

Can't use ARG in docker multistage build

I'm trying to use ARGS in the docker build process multistage.

My Dockerfile looks like:

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/

Whenever it gets the second FROM step in the docker file it fails with the error invalid reference format since it won't recognize the syntax for getting the image.

My assumption is that it doesn't know to handle ARG in a multistage docker build.

Upvotes: 1

Views: 1894

Answers (1)

BMitch
BMitch

Reputation: 263906

ARG's are scoped. Before the first FROM line they are global and available in the FROM lines only. Within each stage, they are scoped until the end of that stage. So you need:

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/

Upvotes: 6

Related Questions