Docker build cache or no cache

stage: Build Image
    - echo "$MY_DEV" > dev_key.json
    - sudo gcloud auth activate-service-account --project $PROJECT_ID_DEV --key-file=dev_key.json
    - sudo gcloud auth configure-docker
    - echo Building the Docker image...
    - sudo docker build --no-cache -t$PROJECT_ID_DEV/$IMAGE_NAME:$IMAGE_TAG --file Dockerfile .
    - sudo docker push$PROJECT_ID_DEV/$IMAGE_NAME:$IMAGE_TAG
    - develop
    - my-runner-ci

I have my pipeline written like above to build image , In this the docker build command by default uses no cache so pipeline is build from start everytime, I want to set an option that by default “docker build should use cache” and want to use no cache manually only whenever I want . I was looking if I can set --no-cache as a variable and then pass by writing some rules? Or any other method if anyone can suggest


Are you sure the docker caching layers are actually used by default?
Remember that caching works only if the image is present locally on the Runner and the image name matches. What I normally do (if I want to use docker caching system) is pull the image first and then use “–cache-from” in the build command.

But, if you don’t have that problem (or have another way to work around it), I believe you could use the power of variables, yes. But this quite depends on the rule - when do you want to cache or not. You can also check out existing list of variables - maybe you find some combo that you can use, then it’s for sure easier.

But this “whenever I want” to me feels more like you should make a CI/CD variable in your project, e.g. USE_DOCKER_CACHE and set it to True or 1 or whatever is easier to work with. Then in the script you can always check that variable and depending on the value proceed, e.g.

    - |
      if [[ "$USE_DOCKER_CACHE" ]]; then # docker cache is set to 1
        echo "Docker cache active"
        docker build -t$PROJECT_ID_DEV/$IMAGE_NAME:$IMAGE_TAG --file Dockerfile .
      else  # docker cache is set to 0
        echo "Docker cache disabled"
        docker build --no-cache -t$PROJECT_ID_DEV/$IMAGE_NAME:$IMAGE_TAG --file Dockerfile .

This way you can manually always go to the project and change that variable when you want.

Best regards!

Thank you for this it worked

Rather than bother the the if-then-else, just plonk a variable on the command line, like so

docker build $DOCKER_BUILD_OPTS -t

If the variable is unset, it expands to an empty string. Otherwise, you just get what you set. So, in order to not use the cache, make sure that


and you’re all set.

Yes! This one crossed my mind as well, thanks for adding :+1:
I know I had a reason why I didn’t put it up in the first place, but right now I cannot remember anymore :sweat_smile: