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 variables declared before the first FROM should only work with FROM instructions #153

Closed
rcjsuen opened this issue Aug 20, 2017 · 2 comments
Assignees
Labels

Comments

@rcjsuen
Copy link
Owner

rcjsuen commented Aug 20, 2017

ARG image=alpine
FROM $image
RUN echo $image
FROM $image

Given the above Dockerfile, hovering over the third line's $image variable will display the value alpine. You would think that to be the case. However, if you try to build the image, you will see that it is actually equivalent to an undeclared variable as nothing is echoed out.

$ docker build .
Sending build context to Docker daemon  86.15MB
Step 1/4 : ARG image=alpine
 --->
Step 2/4 : FROM $image
latest: Pulling from library/alpine
88286f41530e: Pull complete
Digest: sha256:1072e499f3f655a032e88542330cf75b02e7bdf673278f701d7ba61629ee3ebe
Status: Downloaded newer image for alpine:latest
 ---> 7328f6f8b418
Step 3/4 : RUN echo $image
 ---> Running in 45440aa2789c

 ---> e40554735702
Removing intermediate container 45440aa2789c
Step 4/4 : FROM $image
 ---> 7328f6f8b418
Successfully built 7328f6f8b418

moby/moby#34129 has a discussion about this somewhat confusing behaviour. The documentation states the following:

An ARG declared before a FROM is outside of a build stage, so it can't be used in any instruction after a FROM.

I think it's unlikely that the existing behaviour will be modified as existing Dockerfiles may be broken. We should fix how we resolve variables in our hover providers to account for this behaviour.

@rcjsuen rcjsuen added the bug label Aug 20, 2017
@rcjsuen
Copy link
Owner Author

rcjsuen commented Aug 22, 2017

It should be noted that ARG instructions that aren't at the top of the Dockerfile are scoped within a given build stage. The following example illustrates this as the final RUN echo $image instruction ends up printing nothing even though the same instruction on the 4th line did print something.

ARG image=alpine
FROM $image
ARG image=x
RUN echo $image
FROM $image
RUN echo $image
$ docker build .
Sending build context to Docker daemon  86.15MB
Step 1/6 : ARG image=alpine
 --->
Step 2/6 : FROM $image
 ---> 7328f6f8b418
Step 3/6 : ARG image=x
 ---> Running in 1e5b1d7daf50
 ---> f8b94ce82721
Removing intermediate container 1e5b1d7daf50
Step 4/6 : RUN echo $image
 ---> Running in e52c161d1e16
x
 ---> e61f884cee2c
Removing intermediate container e52c161d1e16
Step 5/6 : FROM $image
 ---> 7328f6f8b418
Step 6/6 : RUN echo $image
 ---> Running in def5fca99fa1

 ---> 461c02d4735f
Removing intermediate container def5fca99fa1
Successfully built 461c02d4735f

@rcjsuen rcjsuen changed the title ARG instructions declared before FROM makes their hovers invalid for regular instructions ARG variables declared before the first FROM should only work with FROM instructions Aug 23, 2017
@rcjsuen
Copy link
Owner Author

rcjsuen commented Aug 23, 2017

We'll keep this issue to be solely about the effects of ARG instructions declared before the first FROM. The variable scoping issue described in the earlier comment will be covered by #163.

@rcjsuen rcjsuen self-assigned this Aug 28, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant