[Docker](EN) File download issue caused by layer cache when building docker image using Dockerfile

Post about the case that changed file is not applied due to a cached layer even though the file to be downloaded is changed when building a docker image using Dockerfile


Environment and Prerequisite

  • Linux
  • Docker


Problem and Solution

Issue

  • When building a docker image through a Dockerfile, there is a part to download a file using curl in RUN. At this time, even though the file is changed when a new image is built, the file of the initially cached layer is included so the previous file is included in the image.

Issue Case Example

  • Check file
$ curl https://twpower.me/test.txt
test1
  • Dockerfile example
$ cat Dockerfile
FROM ubuntu:18.04
RUN apt-get update -y
RUN apt-get install curl -y
RUN curl https://twpower.me/test.txt -o test.txt
CMD cat test.txt
  • Build docker image
$ sudo docker build --tag test:1.0 .
  • Run the built image
$ sudo docker run test:1.0
test1
  • Check updated file
$ curl https://twpower.me/test.txt
test2
  • Build docker image with another version
  • Check that the cache is used in below
$ sudo docker build --tag test:2.0 .
Sending build context to Docker daemon  2.048kB
Step 1/5 : FROM ubuntu:18.04
 ---> 56def654ec22
Step 2/5 : RUN apt-get update -y
 ---> Using cache
 ---> 47696d4f4d5f
Step 3/5 : RUN apt-get install curl -y
 ---> Using cache
 ---> 15080e39cb8b
Step 4/5 : RUN curl https://twpower.me/test.txt -o test.txt
 ---> Using cache
 ---> 75bbac2b003f
Step 5/5 : CMD cat test.txt
 ---> Using cache
 ---> 3ce1a5c10a5b
Successfully built 3ce1a5c10a5b
Successfully tagged test:2.0
  • Run the built image
  • The file is changed but the changed file is not applied!
$ sudo docker run test:2.0
test1
$ curl https://twpower.me/test.txt
test2


Cause

  • In Dockerfile, each instruction line is considered as layer and cache is created per that layer. So if specific line and previous lines are same then, it is considered to be a same layer so it load layer from cache if it exists.
  • In above example, RUN curl https://twpower.me/test.txt -o test.txt is same but file is updated.
  • According to official document https://docs.docker.com/develop/develop-images/dockerfile_best-practices/ it says “A Docker image consists of read-only layers each of which represents a Dockerfile instruction. The layers are stacked and each one is a delta of the changes from the previous layer.”.


Solution Methods

Solution 1

  • Use --no-cache to disable the cache using when building docker images
$ sudo docker build --no-cache --tag test:3.0 .

Solution 2

  • (File server side) Tag version to file


Reference