Docker build not using cache when copying Gemfile while using --cache-from

Hey guys,

I’m having an issue with docker build on my CI.

On my local machine, I have built the latest image, and running another docker build uses cache everywhere it should.

Then I upload the image to the registry as the latest, and then on my CI server, I’m pulling the latest image of my app in order to use it as the build cache to build the new version :

docker pull $CONTAINER_IMAGE:latest

docker build --cache-from $CONTAINER_IMAGE:latest \
             --tag $CONTAINER_IMAGE:$CI_COMMIT_SHORT_SHA \
             .

From the build output we can see the COPY of the Gemfile is not using the case from the latest image, while I haven’t updated that file :

Step 15/22 : RUN gem install bundler -v 1.17.3 &&     ln -s /usr/local/lib/ruby/gems/2.2.0/gems/bundler-1.16.0 /usr/local/lib/ruby/gems/2.2.0/gems/bundler-1.16.1
 ---> Using cache
 ---> 47a9ad7747c6
Step 16/22 : ENV BUNDLE_GEMFILE=$APP_HOME/Gemfile     BUNDLE_JOBS=8
 ---> Using cache
 ---> 1124ad337b98
Step 17/22 : WORKDIR $APP_HOME
 ---> Using cache
 ---> 9cd742111641
Step 18/22 : COPY Gemfile $APP_HOME/
 ---> f7ff0ee82ba2
Step 19/22 : COPY Gemfile.lock $APP_HOME/
 ---> c963b4c4617f
Step 20/22 : RUN bundle install
 ---> Running in 3d2cdf999972

Aside node : It is working perfectly on my local machine.

Looking at the Docker documentation Leverage build cache doesn’t seem to explain the behaviour here as nor the Dockerfile, nor the Gemfile have changed, so the cache should be used.

I also tried to copy the files setting the right permissions using COPY --chown=user:group source dest but it still doesn’t use the cache.

What could make Docker not using the cache for the Gemfile?

PS: I have ask this question on Stack Overflow too : https://stackoverflow.com/questions/54574821/docker-build-not-using-cache-when-copying-gemfile-while-using-cache-from

Up

I’m having exactly the same issue and the docs lack of more information, anyone else had the same issue?

I’ve been learning and scratching my head over Docker cache issues with --cache-from for several days, I’ve shared what I’ve learned in the StackOverflow question for this topic: https://stackoverflow.com/a/56024061/149872