11
votes

I'm following the instructions at https://cloud.google.com/container-builder/docs/speeding-up-builds#using_a_cached_docker_image and I'm trying to setup docker builds that use the image cached from the previous build.

Here's what my cloudbuild.yml looks like:

steps:
- name: 'gcr.io/cloud-builders/docker'
  args: ['pull', 'gcr.io/$PROJECT_ID/$REPO_NAME:infra_docker']
- name: 'gcr.io/cloud-builders/docker'
  args: ['build', '--cache-from', 'gcr.io/$PROJECT_ID/$REPO_NAME:infra_docker', '-t', 'gcr.io/$PROJECT_ID/$REPO_NAME:infra_docker', '.']
timeout: 120m
images: ['gcr.io/$PROJECT_ID/$REPO_NAME:infra_docker']
options:
  machineType: 'N1_HIGHCPU_8'

Here's what my Dockerfile starts with:

FROM ubuntu:14.04
SHELL ["/bin/bash", "-c"]
# lots of RUN commands after this

No matter what I try, the docker image pulled from the cache (as a result of the first step), is not used to speed up the actual docker build (second step). It always runs the entire 38 steps in my Dockerfile!

What am I doing wrong?

1
Everything looks right. It might be a silly suggestion, but do you see "Using cache" for each RUN command? or are you sure they are all being executed?Phil
I have the same problem! 8 months (at the time of writing this) and no answer? I've tried 17.09, and 18.06Asfand Qazi
I've been scratching my head over Docker caching issues with --cache-from, I've shared what i've learned here, maybe it can help you to identify the issue: stackoverflow.com/a/56024061/149872 I dunno the specifics, but it sounds like the layers from the pulled image aren't the same anymore as the one you're trying to build (this could be for example if the parent image was updated).Elias Dorneles
Beside RUN are you doing a COPY or ADD? Full Dockerfile may help.Pierre B.
Same problem here currentlyFaultyJuggler

1 Answers

2
votes

Is the dockerfile multi-stage? I ran into this problem where only the final image is available for caching. Depending on the steps you run, this can appear as if no step is using the cache. If this is the case you need to push the intermediate image(s) to the container registry as well and pull them when building.