简体   繁体   中英

Previous apt-get update & install Docker layer removed during every build

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.

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 (eg 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM