0

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?

  • Is that really the entire Dockerfile? seems incomplete – Paolo Sep 14 '20 at 12:46
  • That's the entire dockerfile. The image is created succesfully. I have just simplified the problem – Rajiv Godhwani Sep 14 '20 at 12:47
  • Does this answer your question? [How do I copy variables between stages of multi stage Docker build?](https://stackoverflow.com/questions/52904847/how-do-i-copy-variables-between-stages-of-multi-stage-docker-build) – Paolo Sep 14 '20 at 12:57
  • 1.The arg solution doesn't work. 2.The base solution is not what I want. I can't reuse the same image from the previous stage since it defeats the purpose of optimizing the size of the final image. – Rajiv Godhwani Sep 14 '20 at 13:46

0 Answers0