Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

docker - Does putting ARG at top of Dockerfile prevent layer re-use?

If an ARG that is declared at the top of a Dockerfile gets changed, but its value is only used for a RUN command near the end of the Dockerfile, does Docker rebuild the whole image from scratch or is it able to re-use the intermediate image from right before the relevant RUN command?

To better utilize layering, should I place my ARG declarations at the top of the Dockerfile, or just before the section that uses them?

I guess part of my question is whether or not an ARG directive generates an intermediate layer.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If you change the value of a build argument all of the layers after that ARG line will be invalidated. So I guess you should include it just before you use the ARG.

Just before you need it:

 docker build --build-arg TEST_ARG=test .
 Sending build context to Docker daemon 2.048 kB
 Step 1 : FROM ubuntu
 ---> 104bec311bcd
 Step 2 : RUN echo "no arg used"
 ---> Using cache
 ---> 5c29cb363a27
 Step 3 : ARG TEST_ARG
 ---> Using cache
 ---> 73b6080f973b
 Step 4 : RUN echo $TEST_ARG
 ---> 0acd55c24441
 Successfully built 0acd55c24441

At the top:

docker build --build-arg TEST_ARG=test .
Sending build context to Docker daemon 2.048 kB
Step 1 : FROM ubuntu
 ---> 104bec311bcd
Step 2 : ARG TEST_ARG
---> Using cache
---> b611a1023fe3
Step 3 : RUN echo "no arg used"
---> Running in 63e0f803c6b2
no arg used
---> 592311ccad72
Removing intermediate container 63e0f803c6b2
Step 4 : RUN echo $TEST_ARG
---> Running in 1515aa8702f0
test
---> fc2d850fbbeb
Removing intermediate container 1515aa8702f0
Successfully built fc2d850fbbeb

In the first example two layers are used from the cache and in the second one only one layer (funnily enough, the ARG layer itself) is used from the cache.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...