GoogleContainerTools / kaniko

Build Container Images In Kubernetes
Apache License 2.0
14.88k stars 1.44k forks source link

default docker ARG values ignored when defined after a FROM declaration #2209

Open SamStenton opened 2 years ago

SamStenton commented 2 years ago

Actual behavior When making use of ARGs in a docker file with a default value, the variable default isn't evaluated if defined after a 'FROM' instruction.

Expected behavior Default variables to be evaluated no matter where they're put.

To Reproduce Steps to reproduce the behavior:

This doesn't work

ARG DOCKERFILE_NODE_BUILD_IMAGE_NAME=node
ARG DOCKERFILE_NODE_BUILD_IMAGE_VERSION=alpine

FROM ${DOCKERFILE_NODE_BUILD_IMAGE_NAME}:${DOCKERFILE_NODE_BUILD_IMAGE_VERSION} as node

ARG DOCKERFILE_NGINX_IMAGE_NAME=nginx
ARG DOCKERFILE_NGINX_IMAGE_VERSION=alpine
FROM ${DOCKERFILE_NGINX_IMAGE_NAME}:${DOCKERFILE_NGINX_IMAGE_VERSION} as nginx 

This does work when passing --build-arg DOCKERFILE_NGINX_IMAGE_NAME=otherimage --build-arg DOCKERFILE_NGINX_IMAGE_VERSION=otherversion

ARG DOCKERFILE_NODE_BUILD_IMAGE_NAME=node
ARG DOCKERFILE_NODE_BUILD_IMAGE_VERSION=alpine

FROM ${DOCKERFILE_NODE_BUILD_IMAGE_NAME}:${DOCKERFILE_NODE_BUILD_IMAGE_VERSION} as node

ARG DOCKERFILE_NGINX_IMAGE_NAME=nginx
ARG DOCKERFILE_NGINX_IMAGE_VERSION=alpine
FROM ${DOCKERFILE_NGINX_IMAGE_NAME}:${DOCKERFILE_NGINX_IMAGE_VERSION} as nginx 

This also works without needing to use --build-arg

ARG DOCKERFILE_NODE_BUILD_IMAGE_NAME=node
ARG DOCKERFILE_NODE_BUILD_IMAGE_VERSION=alpine
ARG DOCKERFILE_NGINX_IMAGE_NAME=nginx
ARG DOCKERFILE_NGINX_IMAGE_VERSION=alpine

FROM ${DOCKERFILE_NODE_BUILD_IMAGE_NAME}:${DOCKERFILE_NODE_BUILD_IMAGE_VERSION} as node

FROM ${DOCKERFILE_NGINX_IMAGE_NAME}:${DOCKERFILE_NGINX_IMAGE_VERSION} as nginx 

Additional Information

jbg commented 1 year ago

The last one is inconsistent with Docker, too, isn't it?

ARG DOCKERFILE_NODE_BUILD_IMAGE_NAME=node
ARG DOCKERFILE_NODE_BUILD_IMAGE_VERSION=alpine
ARG DOCKERFILE_NGINX_IMAGE_NAME=nginx
ARG DOCKERFILE_NGINX_IMAGE_VERSION=alpine

FROM ${DOCKERFILE_NODE_BUILD_IMAGE_NAME}:${DOCKERFILE_NODE_BUILD_IMAGE_VERSION} as node

FROM ${DOCKERFILE_NGINX_IMAGE_NAME}:${DOCKERFILE_NGINX_IMAGE_VERSION} as nginx 

In Docker the substitutions in the second FROM wouldn't work unless the ARG lines were repeated after the first FROM, because all ARGs are cleared after each FROM. https://docs.docker.com/engine/reference/builder/#understand-how-arg-and-from-interact