-1

I have a Dockerfile containing the following:

FROM ubuntu:17.10
WORKDIR /app
ADD . /app
RUN apt-get update && apt-get install -y \
python3-pip \
python3-numpy \
ffmpeg \
python3.6 \
xz-utils
...

The layer created at the RUN statement is removed every time I run docker build and I'm not sure why this is the case. Installing all the dependencies takes a long time, so I'd like for Docker to cache that layer and use it again in the future.

What can I do to get that behaviour?

Thank you.

ehdusjenny
  • 146
  • 2
  • 6
  • 2
    Do the contents of the current directory which you're `ADD`ing in also change between builds? – jwodder Dec 30 '17 at 00:17
  • Stack Overflow is a site for programming and development questions. This question appears to be off-topic because it is not about programming or development. See [What topics can I ask about here](http://stackoverflow.com/help/on-topic) in the Help Center. Perhaps [Super User](http://superuser.com/) or [Unix & Linux Stack Exchange](http://unix.stackexchange.com/) would be a better place to ask. – jww Dec 31 '17 at 07:37
  • I'd say that this question about a practical error developing a DockerFile is very much [on topic](https://stackoverflow.com/help/on-topic) since this question covers **1** _software tools commonly used by programmers (ie docker);_ and is **2**_a practical, answerable problem that is unique to software development (why else would you need a docker container?)_. It helped me with my problem at least :shrug:. – Pegasaurus Feb 17 '23 at 00:35

1 Answers1

1

As per documentation, if one layer's cache is invalidated, subsequent layer will need to be re-built. Hence, it's best practice to have all static steps first in Dockerfile (e.g. in your case, you can move RUN apt-get ... command up). Hope it clear

For the ADD and COPY instructions, the contents of the file(s) in the image are examined and a checksum is calculated for each file. The last-modified and last-accessed times of the file(s) are not considered in these checksums. During the cache lookup, the checksum is compared against the checksum in the existing images. If anything has changed in the file(s), such as the contents and metadata, then the cache is invalidated.

Once the cache is invalidated, all subsequent Dockerfile commands will generate new images and the cache will not be used.

sayboras
  • 4,897
  • 2
  • 22
  • 40