Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Jenkins cache dependencies and Docker layers when building?

I'm developing a React app which will be put into a Docker image. I'd like to automate the step of building the image using Jenkins.

This is my current Jenkinsfile...

pipeline {

    agent any

    stages {
        stage('Build') {
            steps {
                script {
                    def commitHash = GIT_COMMIT.take(7)
                    echo 'Building Docker image for commit hash: ' + commitHash
                    def customImage = docker.build("myImage")
                }
            }
        }
    }

}

The image itself is created successfully. I'm now wondering: 1. Will Jenkins download all the dependencies in my package.json with each build? 2. Will Jenkins download all the layers with each build?

Or will Jenkins notice that the dependencies and/or image layers already exist?

like image 943
Robert Strauch Avatar asked Feb 06 '19 16:02

Robert Strauch


1 Answers

TL;DR

It depends on which docker daemon your build agent uses. Each docker daemon has its own cache.


More Detailed Explanation

You are using some plugin (docker-workflow?), which provides the docker.build() step for your pipeline. By default this function tries to use the docker daemon on your agent's localhost (usually trying to use socket /var/run/docker.sock). You can also configure inside the pipeline which docker daemon to use with the docker.withServer() {} block.

The daemon stores/caches your layers, so as long as you use the same daemon across builds then they also all share a common cache.

If you distribute your builds over multiple agents and each agent uses its own localhost docker daemon, then they do not share a common cache.

The official documentation explains this in more detail. Another technique they mention there is to share local volumes on the build agent with your docker image builds (section Caching data for containers). E.g., you could mount the volume with node deps into consecutive docker image builds. That way, even if you clean you docker cache, you do not re-download all node deps on each build.

Still the bottom line is: Jenkins does not cache automagically for you. Caching is inside the scope of the build tool(s) that you are using. You have to take care to incorporate that properly to your CI environment's needs. But of course it is possible to achieve.

Small site note: it might be a good idea to have a clean docker build cache in your CI builds. In the past we had trouble with old docker build caches that corrupted our images. However, if you have a very long build then cache cleaning might not be an option - at least not after every build.

like image 172
fishi0x01 Avatar answered Oct 16 '22 18:10

fishi0x01