Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ARG's are not inherited from a parent stage - only the last stage is considered #116

Closed
Adam-Ant opened this issue Mar 7, 2021 · 7 comments

Comments

@Adam-Ant
Copy link

Adam-Ant commented Mar 7, 2021

When using global ARG's outside of a FROM line (for instance, to make them available to multiple stages), only the last stage is used to generate the arguments property.

For example, this Dockerfile:

ARG GLOBALARG1=foo
ARG GLOBALARG2=bar

FROM python
ARG GLOBALARG1
RUN echo $GLOBALARG1 $GLOBALARG2

FROM python
ARG GLOABLARG2
RUN echo $GLOBALARG2

With this sample script:

from pprint import pprint
from dockerfile_parse import DockerfileParser

dfp = DockerfileParser()

f = open("Dockerfile", "r")
dfp.content = f.read()

pprint(dfp.args)

returns {'GLOABLARG2': ''}, despite the fact that GLOBALARG2 should have inherited from the parent, and its entirely missing GLOBALARG1.

The library successfully detects this is a multistage build, but I can't seem to find a way to present the global arguments, or any arguments from a stage other than the last - as well as arguments not being inherited correctly

@Adam-Ant Adam-Ant changed the title ARG's are not inherited from a parent image - only the last stage is considered ARG's are not inherited from a parent stage - only the last stage is considered Mar 7, 2021
@MartinBasti
Copy link
Contributor

You have typo in Dockerfile

  FROM python
- ARG GLOABLARG2
+ ARG GLOBALARG2
  RUN echo $GLOBALARG2

It prints correct GLOBALARG2

>>> pprint(dfp.args)                                                                                               
{'GLOBALARG2': 'bar'}

dockerfile-parse intentionally returns only values related to the latest stage

@MartinBasti
Copy link
Contributor

Also from docs https://docs.docker.com/engine/reference/builder/#understand-how-arg-and-from-interact

An ARG declared before a FROM is outside of a build stage, so it can’t be used in any instruction after a FROM. To use the default value of an ARG declared before the first FROM use an ARG instruction without a value inside of a build stage

Global ARGs are not available in the stage if there is no empty ARG declaration within stage.

IMO dockerfile-parse works as expected

@frebib
Copy link

frebib commented Mar 8, 2021

Whilst it may be working as intended, we have a legitimate use-case for wanting to set values outside of the last block. Would you accept a PR that keeps the current behaviour, but allows the user to override it with a kwarg?

@MartinBasti
Copy link
Contributor

Can you describe it more? What do you mean by override?

@frebib
Copy link

frebib commented Mar 9, 2021

Sure. We have something akin to the following

ARG NAME=somevalue
ARG ANOTHER="another value"

FROM alpine

ARG NAME
ARG ANOTHER

RUN ..

FROM scratch

ARG NAME

NAME is pulled into both stages, but ANOTHER is not. We'd like to be able to set the value of ANOTHER (on line 2) but with the current logic it's not possible. In fact it's explicitly disallowed:

# extract target instructions from the final stage only

@MartinBasti
Copy link
Contributor

Oh, so you want to be able to modify ARG global definitions before the first FROM?

That sounds good to me

@MartinBasti
Copy link
Contributor

I got broader agreement that this is a good idea, so I have opened 2 issues for that:

Contributions are welcome

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants