5 ms·
I take issue with your use of “magically”. Multistage builds look like a inner scope for each stage, and an outer scope that contains them. In most programmin
by MereInterest 2y ago
I take issue with your use of “magically”. Multistage builds look like a inner scope for each stage, and an outer scope that contains them. In most programming languages, variables in an outer scope are accessible within an inner scope.
I can see an argument for variables declared within one stage being inaccessible in another stage, as the FROM keyword starts a new sibling stage. However, they are still children of the outer scope, should have access to variables in the outer scope. That they do not is rather unexpected.
- imp0cat 2y agoUnderstood, sorry about that. But that's how it feels to me. It's mostly because my mental image of multistage builds is different, I see them as a way to make the final image smaller by throwing away everything from the previous image. So it does make sense to me that ARG values are thrown away after a new FROM instruction and if you need them, you need to declare them again. These two guides have led me to this view, I blame the author! ;) They're excellent BTW, I've revisited them a lot. https://vsupalov.com/docker-arg-env-variable-guide/#arg-and-env https://vsupalov.com/docker-arg-env-variable-guide/#arg-and-... https://vsupalov.com/build-docker-image-clone-private-repo-ssh-key/ https://vsupalov.com/build-docker-image-clone-private-repo-s...
- MereInterest 2y ago> Understood, sorry about that. And apologies on my side, as I worded it a bit strongly. I agree with pretty much everything in your post, except for the implication that it would be magic. To me, the expected behavior is to follow lexical scoping, just like the vast majority of languages from the past several decades. > So it does make sense to me that ARG values are thrown away after a new FROM instruction and if you need them, you need to declare them again. True, and I'd expect an ARG from a previous stage to be undefined when in the next stage. Each ARG belongs to the scope in which it is declared, and a new scope is entered with each FROM statement. However, ARG statements prior to the first FROM statement don't fit into that model. They are before any FROM statement, so they can't belong to any FROM statement. There's a design decision on whether statements before the first FROM statement are a parent scope, or whether they are unrelated scopes, to the scope used for each stage. They feel like they should be a parent scope, because these ARG definitions can be used in a FROM statement, and so it keeps surprising me when they are instead treated as unrelated scopes.