I want to build a docker image. During this process, a massive package (150 MB) needed to be copied from local file system to the image.
I want check the (local directory) existence of this file firstly. If there is, I can COPY it into the image directly. Otherwise I will let docker-build download it from the Internet by a URL and it will take a lot of time.
I call this conditional COPY.
But I don’t know how to implemented this feature.
RUN if
command cannot do this.
2
Answers
You can run if condition in RUN for example
Dockerfile:
Or you can try this
For Copy you can find this dockerfile helpful
I would personally rely on the built-in caching of docker to do this:
For the first time the build will download the file. After that every subsequent build will use the layer cache, as long as you don’t delete the previous images. You can also externalize the build cache with storage backends.
If the instructions before your
ADD
often change the image you can also use the--link
flag forADD
to store this layer independently. See the documentation for ADD –link or better COPY –link.