Does ARG on top of the Dockerfile prevent layer reuse?

If the ARG declared at the top of the Dockerfile is changed, but its value is only used for the RUN command at the end of the Docker file, does Docker recompile the entire image from scratch or can it be reused, use the intermediate image on the right before the corresponding RUN command?

To make better use of layering, should you place ARG declarations at the top of the Docker file, or just before the section that uses them?

I assume that part of my question is whether the ARG directive generates an intermediate layer.

+14
source share
2 answers

, ARG . , ARG.

, :

 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

:

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

, - ( , ARG) .

+10

, , ARG. , ARG RUN. ARG: https://github.com/moby/moby/issues/18017 https://github.com/moby/moby/pull/18161, RUN https://github.com/moby/moby/pull/21885 https://docs.docker.com/engine/reference/builder/#impact-on-build-caching:

ARG , ENV. ARG . Dockerfile ARG, , " ", . , RUN, ARG, ARG ( ), . ARG , Dockerfile ARG.

, , RUN, , .

0

Source: https://habr.com/ru/post/1666505/


All Articles