How to cache data for containers?

0 votes

I am using a maven container,I want to know if there is any method using which i can cache the data whenever the jenkins pipeline runs so that i don't have to re-download various stuff like dependencies every time the pipeline runs?

Jul 9 in Docker by Sam
• 1,070 points
16 views

1 answer to this question.

0 votes

Many build tools like maven etc, will download external dependencies and cache them locally for future re-use. 

Since containers are initially created with "clean" file systems, this can result in slower Pipelines, as they may not take advantage of on-disk caches between subsequent Pipeline runs.

Pipeline supports adding custom arguments which are passed to Docker, allowing users to specify custom docker volumes to mount, which can be used for caching data on the agent between Pipeline runs. 

The following example will cache ~/.m2 between Pipeline runs utilizing the maven container, thereby avoiding the need to re-download dependencies for subsequent runs of the Pipeline.

Jenkinsfile (Declarative Pipeline)

pipeline {
    agent {
        docker {
            image 'maven:3-alpine'
            args '-v $HOME/.m2:/root/.m2'
        }
    }
    stages {
        stage('Build') {
            steps {
                sh 'mvn -B'
            }
        }
    }
}

In case you are using a scripted pipeline here's how you can do it:

Jenkinsfile (Scripted Pipeline)

node {
    /* Requires the Docker Pipeline plugin to be installed */
    docker.image('maven:3-alpine').inside('-v $HOME/.m2:/root/.m2') {
        stage('Build') {
            sh 'mvn -B'
        }
    }
}

answered Jul 9 by Sirajul
• 5,960 points

Related Questions In Docker

0 votes
1 answer
+1 vote
2 answers

How to run docker containers on different machines

You can use labels and selectors for ...READ MORE

answered Oct 23, 2018 in Docker by Laila
56 views
0 votes
1 answer

How to store data in external drive with Docker Postgres:9.3 image?

Apparently, the problem would be in your ...READ MORE

answered Jul 12, 2018 in Docker by Kalgi
• 2,620 points
80 views
0 votes
1 answer

How to obtain the Docker container's IP address from the host?

This can be done by executing the ...READ MORE

answered Jul 16, 2018 in Docker by Sophie may
• 9,430 points
198 views
0 votes
1 answer
0 votes
1 answer

Deploy Docker Containers from Docker Cloud

To solve this problem, I followed advice ...READ MORE

answered Sep 3, 2018 in AWS by Priyaj
• 56,160 points
99 views
0 votes
1 answer

How to move Docker containers from one host to another?

Yes you could definitely do it the ...READ MORE

answered Jul 2 in Docker by Sirajul
• 5,960 points
16 views
0 votes
1 answer

How do I delete the build cache for a docker image?

You could try this inorder to clean ...READ MORE

answered Jul 3 in Docker by Sirajul
• 5,960 points
24 views