I'm tryiing to create a image using multistage and it is not working as I'm expecting.
Consider this docker file:
FROM microsoft/iis:nanoserver
ARG A
RUN echo %A%
FROM mcr.microsoft.com/dotnet/framework/aspnet:4.8
ARG A
RUN echo %A% World
The output I get is
Sending build context to Docker daemon 2.048kB
Step 1/6 : FROM microsoft/iis:nanoserver
---> d4d34a16ef9d
Step 2/6 : ARG A
---> Running in 42eb9102f47e
Removing intermediate container 42eb9102f47e
---> 56403d62db46
Step 3/6 : RUN echo %A%
---> Running in 6867d41abc0d
hello
Removing intermediate container 6867d41abc0d
---> bf0613897813
Step 4/6 : FROM mcr.microsoft.com/dotnet/framework/aspnet:4.8
---> 745ad29a563f
Step 5/6 : ARG A
---> Running in b36c54325d25
Removing intermediate container b36c54325d25
---> b88a59bd8233
Step 6/6 : RUN echo %A% World
---> Running in af036243aaa1
%A%
World
Removing intermediate container af036243aaa1
---> 22dca0b117c3
I'm simply passing the 'A' arg using --build-arg A=hello
As you can see from the output, the value of 'A' is reset in the next stage. But if I use the same base image in the 'FROM' command the value persists to next stage.
Why does this happen? Is this the expected behaviour?
Finally how do I persist the value of 'A' to the next stage?